redis工具类
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisException;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class JedisUtils {
private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
private static String ADDR = "127.0.0.1";
private static String AUTH = "password";
private static int PORT = 6379;
private static int MAX_ACTIVE = 300;
private static int MAX_IDLE = 200;
private static int MAX_WAIT = 10000;
private static int TIMEOUT = 10000;
private static boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static {
try {
init();
} catch (Exception e) {
logger.error("初始化Redis出错," + e);
}
}
private synchronized static void init() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
config.setMaxTotal(MAX_ACTIVE);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
}
public static Object getObject(String key) {
Object value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
value = toObject(jedis.get(getBytesKey(key)));
}
} catch (Exception e) {
logger.info("getObject{} "+ key+"=" + value);
} finally {
returnResource(jedis);
}
return value;
}
public static String get(String key) {
String value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.get(key);
}
} catch (Exception e) {
logger.info("getObject{} "+ key+"=" + value);
} finally {
returnResource(jedis);
}
return value;
}
public static String setObject(String key, Object value, int cacheSeconds) {
String result = null;
Jedis jedis = null;
try {
jedis = getResource();
result = jedis.set(getBytesKey(key), toBytes(value));
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
logger.info("getObject{} "+ key+"=" + value);
} finally {
returnResource(jedis);
}
return result;
}
public static List<Object> getObjectList(String key) {
List<Object> value = null;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
List<byte[]> list = jedis.lrange(getBytesKey(key), 0, -1);
value = Lists.newArrayList();
for (byte[] bs : list){
value.add(toObject(bs));
}
}
} catch (Exception e) {
logger.info("getObjectList {} = {}", key, value);
} finally {
returnResource(jedis);
}
return value;
}
public static long setObjectList(String key, List<Object> value, int cacheSeconds) {
long result = 0;
Jedis jedis = null;
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))) {
jedis.del(key);
}
List<byte[]> list = Lists.newArrayList();
for (Object o : value){
list.add(toBytes(o));
}
for (int i = 0; i < value.size(); i++) {
result = jedis.rpush(getBytesKey(key), getBytesKey(value.get(i)));
}
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
e.