public class RedisUtil {
private static JedisPool pool = null;
/**
* 初始化线程池
*/
static {
JedisPoolConfig config = new JedisPoolConfig();
Setting setting = new Setting("application.properties");
String IP = setting.getStr("redis.IP");
int PORT = setting.getInt("redis.PORT");
int MAX_ACTIVE = setting.getInt("redis.MAX_ACTIVE");
int MAX_IDLE = setting.getInt("redis.MAX_IDLE");
int MAX_WAIT = setting.getInt("redis.MAX_WAIT");
int TIMEOUT = setting.getInt("redis.TIMEOUT");
boolean BORROW = setting.getBool("redis.BORROW");
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(BORROW);
pool = new JedisPool(config, IP, PORT, TIMEOUT);
}
/**
* 获取连接
*
* @return
*/
public static synchronized Jedis getJedis() {
try {
return pool.getResource();
} catch (Exception e) {
return null;
}
}
/**
* @param @param key
* @param @param obj
* @param @param minute
* @return boolean 返回类型
* @Description:插入对象(值序列化为json)
*/
public static boolean setString(String key, Object value, int minute) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.set(key, value.toString());
jedis.expire(key, minute * 60);
return true;
} catch (Exception e) {
return false;
} finally {
jedis.close();
}
}
/**
* @param @param key
* @param @param obj
* @return boolean 返回类型
* @Description:插入对象(值序列化为json)
*/
public static boolean setString(String key, Object value) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.set(key, value.toString());
return true;
} catch (Exception e) {
return false;
} finally {
jedis.close();
}
}
/**
* @param @param key
* @return List<Object> 返回类型
* @Description:获取list
*/
public static List<Object> getList(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
List<Object> list = null;
Long length = jedis.llen(key);
if (length > 0) {
list = Collections.singletonList(jedis.lrange(key, 0, length - 1));
return (List<Object>) list.get(0);
} else {
return null;
}
} catch (Exception e) {
return null;
} finally {
jedis.close();
}
}
/**
* @param @param key
* @param @param obj
* @param @param minute
* @return boolean 返回类型
* @Description:插入列表
*/
public static boolean setList(String key, List<Object> value, int minute) {
Jedis jedis = null;
try {
jedis = getJedis();
if (value != null) {
for (int i = 0, length = value.size(); i < length; i++) {
jedis.lpush(key, value.get(i).toString());
}
}
jedis.expire(key, minute * 60);
return true;
} catch (Exception e) {
return false;
} finally {
jedis.close();
}
}
/**
* @param @param key
* @return boolean 返回类型
* @Description:获取对象
*/
public static String get(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
String code = jedis.get(key);
if (code.equals("[]")) {
return null;
}
return code;
} catch (Exception e) {
return null;
} finally {
jedis.close();
}
}
/**
* @param @param key
* @return boolean 返回类型
* @Description:删除对象
*/
public static boolean remove(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.del(key);
return true;
} catch (Exception e) {
return false;
} finally {
jedis.close();
}
}
/**
* @param @param key
* @return Map<String , String, String>返回类 型
* @Description:模糊查询对象
*/
public static Map<String, String> contains(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
Set<String> set = jedis.keys("*" + key + "*");
Map<String, String> hashTable = new HashMap<>();
if (set != null && set.size() > 0) {
for (String setKey : set) {
hashTable.put(setKey, get(setKey));
}
}
return hashTable;
} catch (Exception e) {
return null;
} finally {
jedis.close();
}
}
/**
* @param @param keys
* @return Map<Object , String> 返回类型
* @Description:模糊查询对象
*/
public static Map<Object, String> contains(Object[] keys) {
Jedis jedis = null;
try {
jedis = getJedis();
Map<Object, String> hashTable = new HashMap<>();
if (keys != null) {
for (int i = 0, len = keys.length; i < len; i++) {
Map<String, String> map = contains(keys[i].toString());
hashTable.putAll(map);
}
}
return hashTable;
} catch (Exception e) {
return null;
} finally {
jedis.close();
}
}
/**
* @param @param key
* @return Long 返回类型
* @Description:插入队列
*/
public static Long lpush(String key, String value) {
Jedis jedis = null;
try {
Long lpush = jedis.lpush(key, value);
return lpush;
} catch (Exception e) {
return null;
} finally {
jedis.close();
}
}
/**
*
@param key
* @param values String[]
* @return 插入队列
*/
public static Long lpush(String key, String[] values){
Jedis jedis = null;
try {
Long lpush = jedis.lpush(key, values);
return lpush;
} catch (Exception e) {
return null;
} finally {
jedis.close();
}
}
}
RedisUtil
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...