importcom.google.common.collect.Lists;importcom.google.common.collect.Maps;importcom.google.common.collect.Sets;importorg.apache.commons.lang3.StringUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;importredis.clients.jedis.exceptions.JedisException;import java.io.*;importjava.lang.reflect.Method;importjava.util.List;importjava.util.Map;importjava.util.Set;/*** Jedis Cache 工具类
*
*@authorhpc
*@version2017-8-29*/
public classJedisUtils {private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);/*** 链接url*/
//如果是本机,则用默认的的127地址,远程连接用对方ip//private static String ADDR = "127.0.0.1";
private static String ADDR = "10.101.148.11";private static int PORT = 6379;/*** 密码*/
private static String AUTH = null;//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;/*** 初始化Redis连接池*/
static{try{
init();
}catch(Exception e) {
logger.error("初始化Redis出错," +e);
}
}/*** 初始化连接池
*
*@see[类、类#方法、类#成员]*/
private synchronized static voidinit() {
JedisPoolConfig config= newJedisPoolConfig();//config.setMaxIdle(MAX_IDLE);//config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);//config.setMaxTotal(MAX_ACTIVE);
jedisPool = newJedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
}/*** 获取缓存
*@paramkey 键
*@return值*/
public staticString get(String key) {
String value= null;
Jedis jedis= null;try{
jedis=getResource();if(jedis.exists(key)) {
value=jedis.get(key);
value= StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
logger.debug("get {} = {}", key, value);
}
}catch(Exception e) {
logger.error("get {} = {} error = {}", key, value);
}finally{
returnResource(jedis);
}returnvalue;
}/*** 获取缓存
*@paramkey 键
*@return值*/
public staticObject getObject(String key) {
Object value= null;
Jedis jedis= null;try{
jedis=getResource();if(jedis.exists(getBytesKey(key))) {
value=toObject(jedis.get(getBytesKey(key)));
logger.debug("getObject {} = {}", key, value);
}
}catch(Exception e) {
logger.warn("getObject{} "+ key+"=" +value);
}finally{
returnResource(jedis);
}returnvalue;
}/*** 设置缓存
*@paramkey 键
*@paramvalue 值
*@paramcacheSeconds 超时时间,0为不超时 以秒为单位
*@return
*/
public static String set(String key, String value, intcacheSeconds) {
String result= null;
Jedis jedis= null;try{
jedis=getResource();
result=jedis.set(key, value);if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("set {} = {}", key, value);
}catch(Exception e) {//logger.warn("set {} = {}", key, value, e);
} finally{
returnResource(jedis);
}returnresult;
}/*** 设置缓存
*@paramkey 键
*@paramvalue 值
*@paramcacheSeconds 超时时间,0为不超时
*@return
*/
public static String setObject(String key, Object value, intcacheSeconds) {
String result= null;
Jedis jedis= null;try{
jedis=getResource();
result=jedis.set(getBytesKey(key), toBytes(value));if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("setObject {} = {}", key, value);
}catch(Exception e) {
logger.warn("getObject{} "+ key+"=" +value);
}finally{
returnResource(jedis);
}returnresult;
}/*** 获取List缓存
*@paramkey 键
*@return值*/
public static ListgetList(String key) {
List value = null;
Jedis jedis= null;try{
jedis=getResource();if(jedis.exists(key)) {
value= jedis.lrange(key, 0, -1);
logger.debug("getList {} = {}", key, value);
}
}catch(Exception e) {//logger.warn("getList {} = {}", key, value, e);
logger.warn("getList{} "+ key+"=" +value);
}finally{
returnResource(jedis);
}returnvalue;
}/*** 获取List缓存
*@paramkey 键
*@return值*/
public static ListgetObjectList(String key) {
List value = null;
Jedis jedis= null;try{
jedis=getResource();if(jedis.exists(getBytesKey(key))) {
List list = jedis.lrange(getBytesKey(key), 0, -1);
value=Lists.newArrayList();for (byte[] bs :