|
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -23,272 +24,304 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Service
|
|
|
public class RedisService {
|
|
|
|
|
|
- @Resource
|
|
|
- private RedisTemplate<Serializable, Object> redisTemplate;
|
|
|
+ @Resource
|
|
|
+ private RedisTemplate<Serializable, Object> redisTemplate;
|
|
|
|
|
|
/**
|
|
|
* 批量删除
|
|
|
+ *
|
|
|
* @param keys
|
|
|
*/
|
|
|
- public void remove(String... keys) {
|
|
|
- for(String key :keys){
|
|
|
- remove(key);
|
|
|
- }
|
|
|
- }
|
|
|
+ public void remove(String... keys) {
|
|
|
+ for (String key : keys) {
|
|
|
+ remove(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 批量删除正则匹配到的
|
|
|
+ *
|
|
|
* @param pattern
|
|
|
*/
|
|
|
- public void removePattern(String pattern) {
|
|
|
- Set<Serializable> keys = redisTemplate.keys(pattern);
|
|
|
+ public void removePattern(String pattern) {
|
|
|
+ Set<Serializable> keys = redisTemplate.keys(pattern);
|
|
|
assert keys != null;
|
|
|
- if (keys.size() > 0){
|
|
|
- redisTemplate.delete(keys);
|
|
|
- }
|
|
|
- }
|
|
|
+ if (keys.size() > 0) {
|
|
|
+ redisTemplate.delete(keys);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 删除
|
|
|
+ *
|
|
|
* @param key
|
|
|
*/
|
|
|
- public void remove(String key) {
|
|
|
- if (exists(key)) {
|
|
|
- redisTemplate.delete(key);
|
|
|
- }
|
|
|
- }
|
|
|
+ public void remove(String key) {
|
|
|
+ if (exists(key)) {
|
|
|
+ redisTemplate.delete(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 判断缓存中是否存在
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @return boolean
|
|
|
*/
|
|
|
- public boolean exists(String key) {
|
|
|
- return StringUtils.isBlank(key) ? false : redisTemplate.hasKey(key);
|
|
|
- }
|
|
|
+ public boolean exists(String key) {
|
|
|
+ return StringUtils.isBlank(key) ? false : redisTemplate.hasKey(key);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 读取缓存
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
- public Object get(String key) {
|
|
|
- Object result = null;
|
|
|
- ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
- result = operations.get(key);
|
|
|
- return result;
|
|
|
- }
|
|
|
+ public Object get(String key) {
|
|
|
+ Object result = null;
|
|
|
+ ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
+ result = operations.get(key);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 写入缓存
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param value
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean set(String key, Object value) {
|
|
|
- boolean result = false;
|
|
|
- try {
|
|
|
- ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
- operations.set(key, value);
|
|
|
- result = true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
+ public boolean set(String key, Object value) {
|
|
|
+ boolean result = false;
|
|
|
+ try {
|
|
|
+ ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
+ operations.set(key, value);
|
|
|
+ result = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 写入缓存并加上过期时间(秒)
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param value
|
|
|
* @param expireTimeSeconds
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean set(String key, Object value, Long expireTimeSeconds) {
|
|
|
- boolean result = false;
|
|
|
- try {
|
|
|
- ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
- operations.set(key, value);
|
|
|
- redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
|
|
|
- result = true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
+ public boolean set(String key, Object value, Long expireTimeSeconds) {
|
|
|
+ boolean result = false;
|
|
|
+ try {
|
|
|
+ ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
+ operations.set(key, value);
|
|
|
+ redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
|
|
|
+ result = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 写入过期时间(秒)
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param expireTimeSeconds
|
|
|
* @return
|
|
|
*/
|
|
|
- public boolean expire(String key, Long expireTimeSeconds) {
|
|
|
- boolean result = false;
|
|
|
- try {
|
|
|
- redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
|
|
|
- result = true;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
+ public boolean expire(String key, Long expireTimeSeconds) {
|
|
|
+ boolean result = false;
|
|
|
+ try {
|
|
|
+ redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
|
|
|
+ result = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
/* **************************** 针对list操作的方法 **************************** */
|
|
|
+
|
|
|
/**
|
|
|
* 在key对应list的尾部添加
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param value
|
|
|
* @return
|
|
|
*/
|
|
|
- public long rightPushForList(String key, Object value) {
|
|
|
- return redisTemplate.opsForList().rightPush(key, value);
|
|
|
- }
|
|
|
+ public long rightPushForList(String key, Object value) {
|
|
|
+ return redisTemplate.opsForList().rightPush(key, value);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 在key对应list的头部添加
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param value
|
|
|
* @return
|
|
|
*/
|
|
|
- public long leftPushForList(String key, Object value) {
|
|
|
- return redisTemplate.opsForList().leftPush(key, value);
|
|
|
- }
|
|
|
+ public long leftPushForList(String key, Object value) {
|
|
|
+ return redisTemplate.opsForList().leftPush(key, value);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* key对应list的长度
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
- public long listSize(String key) {
|
|
|
- return redisTemplate.opsForList().size(key);
|
|
|
- }
|
|
|
+ public long listSize(String key) {
|
|
|
+ return redisTemplate.opsForList().size(key);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 获取list集合
|
|
|
+ *
|
|
|
* @param Key
|
|
|
* @param begin
|
|
|
* @param end
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<?> getList(String Key, int begin, int end) {
|
|
|
- return redisTemplate.opsForList().range(Key, begin, end);
|
|
|
- }
|
|
|
+ public List<?> getList(String Key, int begin, int end) {
|
|
|
+ return redisTemplate.opsForList().range(Key, begin, end);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 在key对应list的尾部移除
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
- public Object rightPopForList(String key) {
|
|
|
- return redisTemplate.opsForList().rightPop(key);
|
|
|
- }
|
|
|
+ public Object rightPopForList(String key) {
|
|
|
+ return redisTemplate.opsForList().rightPop(key);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 在key对应list的头部移除
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
- public Object leftPopForList(String key) {
|
|
|
- return redisTemplate.opsForList().leftPop(key);
|
|
|
- }
|
|
|
+ public Object leftPopForList(String key) {
|
|
|
+ return redisTemplate.opsForList().leftPop(key);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 移除list的index位置上的值
|
|
|
+ *
|
|
|
* @param Key
|
|
|
* @param index
|
|
|
* @param value
|
|
|
*/
|
|
|
- public void removeList(String Key, long index, Object value) {
|
|
|
- redisTemplate.opsForList().remove(Key, index, value);
|
|
|
- }
|
|
|
+ public void removeList(String Key, long index, Object value) {
|
|
|
+ redisTemplate.opsForList().remove(Key, index, value);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 重置list的index位置上的值
|
|
|
+ *
|
|
|
* @param Key
|
|
|
* @param index
|
|
|
* @param value
|
|
|
*/
|
|
|
- public void setList(String Key, long index, Object value) {
|
|
|
- redisTemplate.opsForList().set(Key, index, value);
|
|
|
- }
|
|
|
+ public void setList(String Key, long index, Object value) {
|
|
|
+ redisTemplate.opsForList().set(Key, index, value);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 写入list
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param list
|
|
|
*/
|
|
|
- public void setList(String key, List list) {
|
|
|
- if(list!=null&&list.size()>0){
|
|
|
- for (Object object : list) {
|
|
|
- rightPushForList(key,object);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ public void setList(String key, List list) {
|
|
|
+ if (list != null && list.size() > 0) {
|
|
|
+ for (Object object : list) {
|
|
|
+ rightPushForList(key, object);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 写入map
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param map
|
|
|
*/
|
|
|
- public void setMap(String key, Map<String, Object> map) {
|
|
|
- redisTemplate.opsForHash().putAll(key, map);
|
|
|
- }
|
|
|
+ public void setMap(String key, Map<String, Object> map) {
|
|
|
+ redisTemplate.opsForHash().putAll(key, map);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 获取map
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param mapKey
|
|
|
* @return
|
|
|
*/
|
|
|
- public Object get(String key, String mapKey) {
|
|
|
- return redisTemplate.opsForHash().get(key, mapKey);
|
|
|
- }
|
|
|
+ public Object get(String key, String mapKey) {
|
|
|
+ return redisTemplate.opsForHash().get(key, mapKey);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 写入map
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param hashKey
|
|
|
* @param hashValue
|
|
|
*/
|
|
|
- public void setMapByKV(String key, Object hashKey, Object hashValue) {
|
|
|
- redisTemplate.opsForHash().put(key, hashKey,hashValue);
|
|
|
- }
|
|
|
+ public void setMapByKV(String key, Object hashKey, Object hashValue) {
|
|
|
+ redisTemplate.opsForHash().put(key, hashKey, hashValue);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 删除map中的某个key-value
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param hashKey
|
|
|
*/
|
|
|
- public void removeHash(String key, String hashKey) {
|
|
|
- redisTemplate.opsForHash().delete(key, hashKey);
|
|
|
- }
|
|
|
+ public void removeHash(String key, String hashKey) {
|
|
|
+ redisTemplate.opsForHash().delete(key, hashKey);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
- public Map<Object, Object> getEntries(String key) {
|
|
|
- return redisTemplate.opsForHash().entries(key);
|
|
|
- }
|
|
|
+ public Map<Object, Object> getEntries(String key) {
|
|
|
+ return redisTemplate.opsForHash().entries(key);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
* @param key
|
|
|
* @param step
|
|
|
* @return
|
|
|
*/
|
|
|
- public long increase(String key, long step) {
|
|
|
- return redisTemplate.opsForValue().increment(key, step);
|
|
|
- }
|
|
|
+ public long increase(String key, long step) {
|
|
|
+ return redisTemplate.opsForValue().increment(key, step);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 获取失效时间
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
- public long getExpireTime(String key) {
|
|
|
- return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
+ public long getExpireTime(String key) {
|
|
|
+ return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
|
|
|
+ public Object getManagerKey(String pKey) {
|
|
|
+ StringRedisSerializer keySerializer = new StringRedisSerializer();
|
|
|
+ StringRedisSerializer valueSerializer = new StringRedisSerializer();
|
|
|
+ redisTemplate.setKeySerializer(keySerializer);
|
|
|
+ redisTemplate.setValueSerializer(valueSerializer);
|
|
|
+ Object result = null;
|
|
|
+ ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
+ result = operations.get(pKey);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|