|
@@ -28,15 +28,10 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean expire(String key, long time) {
|
|
|
- try {
|
|
|
- if (time > 0) {
|
|
|
- redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
+ if (time > 0) {
|
|
|
+ redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 根据key 获取过期时间
|
|
@@ -54,12 +49,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return true 存在 false不存在
|
|
|
*/
|
|
|
public boolean hasKey(String key) {
|
|
|
- try {
|
|
|
- return redisTemplate.hasKey(key);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ return redisTemplate.hasKey(key);
|
|
|
}
|
|
|
/**
|
|
|
* 删除缓存
|
|
@@ -94,13 +84,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return true成功 false失败
|
|
|
*/
|
|
|
public boolean set(String key, String value) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForValue().set(key, value);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForValue().set(key, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 普通缓存放入并设置时间
|
|
@@ -111,17 +96,12 @@ public class RedisUtil<K, V>{
|
|
|
* @return true成功 false 失败
|
|
|
*/
|
|
|
public boolean set(String key, String value, long time) {
|
|
|
- try {
|
|
|
- if (time > 0) {
|
|
|
- redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
|
|
- } else {
|
|
|
- set(key, value);
|
|
|
- }
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
+ if (time > 0) {
|
|
|
+ redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
|
|
+ } else {
|
|
|
+ set(key, value);
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 递增
|
|
@@ -203,13 +183,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return true 成功 false 失败
|
|
|
*/
|
|
|
public boolean hmset(String key, Map<String, Object> map) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForHash().putAll(key, map);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForHash().putAll(key, map);
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* HashSet 并设置时间
|
|
@@ -220,16 +195,11 @@ public class RedisUtil<K, V>{
|
|
|
* @return true成功 false失败
|
|
|
*/
|
|
|
public boolean hmset(String key, Map<String, Object> map, long time) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForHash().putAll(key, map);
|
|
|
- if (time > 0) {
|
|
|
- expire(key, time);
|
|
|
- }
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
+ redisTemplate.opsForHash().putAll(key, map);
|
|
|
+ if (time > 0) {
|
|
|
+ expire(key, time);
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 向一张hash表中放入数据,如果不存在将创建
|
|
@@ -240,13 +210,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return true 成功 false失败
|
|
|
*/
|
|
|
public boolean hset(String key, String item, Object value) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForHash().put(key, item, value);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForHash().put(key, item, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 向一张hash表中放入数据,如果不存在将创建
|
|
@@ -258,16 +223,11 @@ public class RedisUtil<K, V>{
|
|
|
* @return true 成功 false失败
|
|
|
*/
|
|
|
public boolean hset(String key, String item, Object value, long time) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForHash().put(key, item, value);
|
|
|
- if (time > 0) {
|
|
|
- expire(key, time);
|
|
|
- }
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
+ redisTemplate.opsForHash().put(key, item, value);
|
|
|
+ if (time > 0) {
|
|
|
+ expire(key, time);
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 删除hash表中的值
|
|
@@ -318,12 +278,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public Set<Object> sGet(String key) {
|
|
|
- try {
|
|
|
- return redisTemplate.opsForSet().members(key);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForSet().members(key);
|
|
|
}
|
|
|
/**
|
|
|
* 根据value从一个set中查询,是否存在
|
|
@@ -333,12 +288,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return true 存在 false不存在
|
|
|
*/
|
|
|
public boolean sHasKey(String key, Object value) {
|
|
|
- try {
|
|
|
- return redisTemplate.opsForSet().isMember(key, value);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForSet().isMember(key, value);
|
|
|
}
|
|
|
/**
|
|
|
* 将数据放入set缓存
|
|
@@ -348,12 +298,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return 成功个数
|
|
|
*/
|
|
|
public long sSet(String key, Object... values) {
|
|
|
- try {
|
|
|
- return redisTemplate.opsForSet().add(key, values);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForSet().add(key, values);
|
|
|
}
|
|
|
/**
|
|
|
* 将set数据放入缓存
|
|
@@ -364,15 +309,10 @@ public class RedisUtil<K, V>{
|
|
|
* @return 成功个数
|
|
|
*/
|
|
|
public long sSet(String key, long time, Object... values) {
|
|
|
- try {
|
|
|
- Long count = redisTemplate.opsForSet().add(key, values);
|
|
|
- if (time > 0)
|
|
|
- expire(key, time);
|
|
|
- return count;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ Long count = redisTemplate.opsForSet().add(key, values);
|
|
|
+ if (time > 0)
|
|
|
+ expire(key, time);
|
|
|
+ return count;
|
|
|
}
|
|
|
/**
|
|
|
* 获取set缓存的长度
|
|
@@ -381,13 +321,19 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public long sGetSize(String key) {
|
|
|
- try {
|
|
|
- return redisTemplate.opsForSet().size(key);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForSet().size(key);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取set缓存的长度
|
|
|
+ *
|
|
|
+ * @param key 键
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean sExists(String key, Object value) {
|
|
|
+ return redisTemplate.opsForSet().isMember(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 移除值为value的
|
|
|
*
|
|
@@ -396,13 +342,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return 移除的个数
|
|
|
*/
|
|
|
public long setRemove(String key, Object... values) {
|
|
|
- try {
|
|
|
- Long count = redisTemplate.opsForSet().remove(key, values);
|
|
|
- return count;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForSet().remove(key, values);
|
|
|
}
|
|
|
// ===============================list=================================
|
|
|
/**
|
|
@@ -414,13 +354,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Object> lGet(String key, long start, long end) {
|
|
|
- try {
|
|
|
- List<Object> range = redisTemplate.opsForList().range(key, start, end);
|
|
|
- return range;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForList().range(key, start, end);
|
|
|
}
|
|
|
/**
|
|
|
* 获取list缓存的长度
|
|
@@ -429,12 +363,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public long lGetSize(String key) {
|
|
|
- try {
|
|
|
- return redisTemplate.opsForList().size(key);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForList().size(key);
|
|
|
}
|
|
|
/**
|
|
|
* 通过索引 获取list中的值
|
|
@@ -444,12 +373,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public Object lGetIndex(String key, long index) {
|
|
|
- try {
|
|
|
- return redisTemplate.opsForList().index(key, index);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForList().index(key, index);
|
|
|
}
|
|
|
/**
|
|
|
* 将list放入缓存
|
|
@@ -458,13 +382,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean lRightPush(String key, Object value) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForList().rightPush(key, value);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForList().rightPush(key, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 将list放入缓存
|
|
@@ -475,15 +394,10 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean lRightPush(String key, Object value, long time) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForList().rightPush(key, value);
|
|
|
- if (time > 0)
|
|
|
- expire(key, time);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForList().rightPush(key, value);
|
|
|
+ if (time > 0)
|
|
|
+ expire(key, time);
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 将list放入缓存
|
|
@@ -493,13 +407,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean lRightPushAll(String key, List<Object> value) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForList().rightPushAll(key, value);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForList().rightPushAll(key, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -511,15 +420,10 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean lRightPushAll(String key, List<Object> value, long time) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForList().rightPushAll(key, value);
|
|
|
- if (time > 0)
|
|
|
- expire(key, time);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForList().rightPushAll(key, value);
|
|
|
+ if (time > 0)
|
|
|
+ expire(key, time);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -530,13 +434,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean lLeftPushAll(String key, List<Object> value) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForList().leftPushAll(key, value);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForList().leftPushAll(key, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -547,13 +446,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean lLeftPush(String key, String value) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForList().leftPush(key, value);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForList().leftPush(key, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -562,12 +456,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public String lLeftPop(String key) {
|
|
|
- try {
|
|
|
- return (String)redisTemplate.opsForList().leftPop(key);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
+ return (String)redisTemplate.opsForList().leftPop(key);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -579,13 +468,8 @@ public class RedisUtil<K, V>{
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean lUpdateByIndex(String key, long index, Object value) {
|
|
|
- try {
|
|
|
- redisTemplate.opsForList().set(key, index, value);
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
+ redisTemplate.opsForList().set(key, index, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
/**
|
|
|
* 移除N个值为value
|
|
@@ -596,13 +480,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return 移除的个数
|
|
|
*/
|
|
|
public long lRemove(String key, long count, Object value) {
|
|
|
- try {
|
|
|
- Long remove = redisTemplate.opsForList().remove(key, count, value);
|
|
|
- return remove;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ return redisTemplate.opsForList().remove(key, count, value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -612,12 +490,7 @@ public class RedisUtil<K, V>{
|
|
|
* @return 移除的个数
|
|
|
*/
|
|
|
public Set<String> keys(String pattern) {
|
|
|
- try {
|
|
|
- return redisTemplate.keys(pattern);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return new HashSet<>();
|
|
|
- }
|
|
|
+ return redisTemplate.keys(pattern);
|
|
|
}
|
|
|
|
|
|
public void expire(String key,Duration time){
|