|
|
@@ -9,32 +9,17 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
/**
|
|
|
* spring redis 工具类
|
|
|
*
|
|
|
|
|
|
**/
|
|
|
+@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
|
|
@Component
|
|
|
public class RedisCache
|
|
|
{
|
|
|
@Autowired
|
|
|
public RedisTemplate redisTemplate;
|
|
|
- /**
|
|
|
- * 递增 key 对应的数值
|
|
|
- *
|
|
|
- * @param key 缓存键
|
|
|
- * @param delta 增量
|
|
|
- * @return 递增后的值
|
|
|
- */
|
|
|
- public Long increment(final String key, final long delta) {
|
|
|
- return redisTemplate.opsForValue().increment(key, delta);
|
|
|
- }
|
|
|
- public Long incrementCacheValue(final String key, final long delta) {
|
|
|
- return redisTemplate.opsForValue().increment(key, delta);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 缓存基本的对象,Integer、String、实体类等
|
|
|
@@ -85,18 +70,6 @@ public class RedisCache
|
|
|
return redisTemplate.expire(key, timeout, unit);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 当 key 不存在时设置值
|
|
|
- *
|
|
|
- * @param key 缓存键
|
|
|
- * @param value 缓存值
|
|
|
- * @param timeout 过期时间
|
|
|
- * @param unit 时间单位
|
|
|
- * @return true: 设置成功,false: key 已存在
|
|
|
- */
|
|
|
- public boolean setIfAbsent(final String key, final Object value, long timeout, TimeUnit unit) {
|
|
|
- return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit));
|
|
|
- }
|
|
|
/**
|
|
|
* 获得缓存的基本对象。
|
|
|
*
|
|
|
@@ -143,46 +116,6 @@ public class RedisCache
|
|
|
return count == null ? 0 : count;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加List消息数据
|
|
|
- *
|
|
|
- * @param key 缓存的键值
|
|
|
- * @param dataList 待缓存的List消息数据
|
|
|
- * @return 缓存的对象
|
|
|
- */
|
|
|
- public <T> long setVoiceList(final String key, final List<T> dataList)
|
|
|
- {
|
|
|
- Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
|
|
|
- return count == null ? 0 : count;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加List消息数据的key
|
|
|
- *
|
|
|
- * @param key 缓存的键值
|
|
|
- * @param value 待缓存的值
|
|
|
- * @return 缓存的对象
|
|
|
- */
|
|
|
- public <T> void setVoice(final String key, final T value)
|
|
|
- {
|
|
|
- redisTemplate.opsForList().rightPush(key, value);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获得list对象的value
|
|
|
- *
|
|
|
- * @param key 缓存的键值
|
|
|
- * @return 缓存键值对应的数据
|
|
|
- */
|
|
|
- public <T> T getVoiceAllList(final String key)
|
|
|
- {
|
|
|
- return (T) redisTemplate.opsForList().range(key,0,-1);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 获得缓存的list对象
|
|
|
*
|
|
|
@@ -295,25 +228,6 @@ public class RedisCache
|
|
|
return redisTemplate.keys(pattern);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 将对象添加到 Redis 的 Set 集合中
|
|
|
- *
|
|
|
- * @param key Redis Key
|
|
|
- * @param value 要添加的对象(例如 userId)
|
|
|
- */
|
|
|
- public void setSetCacheObject(String key, Object value) {
|
|
|
- redisTemplate.opsForSet().add(key, value);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 从 Redis 的 Set 集合中移除指定的对象
|
|
|
- *
|
|
|
- * @param key Redis Key
|
|
|
- * @param value 要移除的对象(例如 userId)
|
|
|
- */
|
|
|
- public void removeSetCacheObject(String key, Object value) {
|
|
|
- redisTemplate.opsForSet().remove(key, value);
|
|
|
- }
|
|
|
|
|
|
public Boolean setIfAbsent(String key, String value, long timeout, TimeUnit unit) {
|
|
|
return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit);
|
|
|
@@ -339,7 +253,6 @@ public class RedisCache
|
|
|
return redisTemplate.opsForValue().increment(key, delta);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 获取字符串值
|
|
|
*
|