修改问题
This commit is contained in:
+498
-495
@@ -1,495 +1,498 @@
|
||||
package com.central.common.redis.template;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisClusterNode;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisServerCommands;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Redis Repository
|
||||
* redis 基本操作 可扩展,基本够用了
|
||||
*
|
||||
* @author zlt
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisRepository {
|
||||
/**
|
||||
* Spring Redis Template
|
||||
*/
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
public RedisRepository(RedisTemplate<String, Object> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取链接工厂
|
||||
*/
|
||||
public RedisConnectionFactory getConnectionFactory() {
|
||||
return this.redisTemplate.getConnectionFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 RedisTemplate对象
|
||||
*/
|
||||
public RedisTemplate<String, Object> getRedisTemplate() {
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空DB
|
||||
*
|
||||
* @param node redis 节点
|
||||
*/
|
||||
public void flushDB(RedisClusterNode node) {
|
||||
this.redisTemplate.opsForCluster().flushDb(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到带有 过期时间的 缓存
|
||||
*
|
||||
* @param key redis主键
|
||||
* @param value 值
|
||||
* @param time 过期时间(单位秒)
|
||||
*/
|
||||
public void setExpire(final byte[] key, final byte[] value, final long time) {
|
||||
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
||||
connection.setEx(key, time, value);
|
||||
return 1L;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到带有 过期时间的 缓存
|
||||
*
|
||||
* @param key redis主键
|
||||
* @param value 值
|
||||
* @param time 过期时间
|
||||
* @param timeUnit 过期时间单位
|
||||
*/
|
||||
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit) {
|
||||
redisTemplate.opsForValue().set(key, value, time, timeUnit);
|
||||
}
|
||||
public void setExpire(final String key, final Object value, final long time) {
|
||||
this.setExpire(key, value, time, TimeUnit.SECONDS);
|
||||
}
|
||||
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
byte[] rawValue = rawValue(value, valueSerializer);
|
||||
|
||||
redisTemplate.execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
potentiallyUsePsetEx(connection);
|
||||
return null;
|
||||
}
|
||||
public void potentiallyUsePsetEx(RedisConnection connection) {
|
||||
if (!TimeUnit.MILLISECONDS.equals(timeUnit) || !failsafeInvokePsetEx(connection)) {
|
||||
connection.setEx(rawKey, TimeoutUtils.toSeconds(time, timeUnit), rawValue);
|
||||
}
|
||||
}
|
||||
private boolean failsafeInvokePsetEx(RedisConnection connection) {
|
||||
boolean failed = false;
|
||||
try {
|
||||
connection.pSetEx(rawKey, time, rawValue);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
failed = true;
|
||||
}
|
||||
return !failed;
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
|
||||
*
|
||||
* @param keys redis主键数组
|
||||
* @param values 值数组
|
||||
* @param time 过期时间(单位秒)
|
||||
*/
|
||||
public void setExpire(final String[] keys, final Object[] values, final long time) {
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
redisTemplate.opsForValue().set(keys[i], values[i], time, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
|
||||
*
|
||||
* @param keys the keys
|
||||
* @param values the values
|
||||
*/
|
||||
public void set(final String[] keys, final Object[] values) {
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
redisTemplate.opsForValue().set(keys[i], values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加到缓存
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
public void set(final String key, final Object value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询在以keyPatten的所有 key
|
||||
*
|
||||
* @param keyPatten the key patten
|
||||
* @return the set
|
||||
*/
|
||||
public Set<String> keys(final String keyPatten) {
|
||||
return redisTemplate.keys(keyPatten + "*");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key获取对象
|
||||
*
|
||||
* @param key the key
|
||||
* @return the byte [ ]
|
||||
*/
|
||||
public byte[] get(final byte[] key) {
|
||||
return redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key获取对象
|
||||
*
|
||||
* @param key the key
|
||||
* @return the string
|
||||
*/
|
||||
public Object get(final String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
*获取原来key键对应的值并重新赋新值。
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public String getAndSet(final String key,Object value) {
|
||||
String result = null;
|
||||
if (StringUtils.isEmpty(key)){
|
||||
log.error("key不能为空");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
result = (String) redisTemplate.opsForValue().getAndSet(key, value);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 根据key获取对象
|
||||
*
|
||||
* @param key the key
|
||||
* @param valueSerializer 序列化
|
||||
* @return the string
|
||||
*/
|
||||
public Object get(final String key, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
return redisTemplate.execute(connection -> deserializeValue(connection.get(rawKey), valueSerializer), true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ops for hash hash operations.
|
||||
*
|
||||
* @return the hash operations
|
||||
*/
|
||||
public HashOperations<String, String, Object> opsForHash() {
|
||||
return redisTemplate.opsForHash();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对HashMap操作
|
||||
*
|
||||
* @param key the key
|
||||
* @param hashKey the hash key
|
||||
* @param hashValue the hash value
|
||||
*/
|
||||
public void putHashValue(String key, String hashKey, Object hashValue) {
|
||||
opsForHash().put(key, hashKey, hashValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单个field对应的值
|
||||
*
|
||||
* @param key the key
|
||||
* @param hashKey the hash key
|
||||
* @return the hash values
|
||||
*/
|
||||
public Object getHashValues(String key, String hashKey) {
|
||||
return opsForHash().get(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key值删除
|
||||
*
|
||||
* @param key the key
|
||||
* @param hashKeys the hash keys
|
||||
*/
|
||||
public void delHashValues(String key, Object... hashKeys) {
|
||||
opsForHash().delete(key, hashKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* key只匹配map
|
||||
*
|
||||
* @param key the key
|
||||
* @return the hash value
|
||||
*/
|
||||
public Map<String, Object> getHashValue(String key) {
|
||||
return opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加
|
||||
*
|
||||
* @param key the key
|
||||
* @param map the map
|
||||
*/
|
||||
public void putHashValues(String key, Map<String, Object> map) {
|
||||
opsForHash().putAll(key, map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合数量
|
||||
*
|
||||
* @return the long
|
||||
*/
|
||||
public long dbSize() {
|
||||
return redisTemplate.execute(RedisServerCommands::dbSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空redis存储的数据
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String flushDB() {
|
||||
return redisTemplate.execute((RedisCallback<String>) connection -> {
|
||||
connection.flushDb();
|
||||
return "ok";
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断某个主键是否存在
|
||||
*
|
||||
* @param key the key
|
||||
* @return the boolean
|
||||
*/
|
||||
public boolean exists(final String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除key
|
||||
*
|
||||
* @param keys the keys
|
||||
* @return the long
|
||||
*/
|
||||
public boolean del(final String... keys) {
|
||||
boolean result = false;
|
||||
for (String key : keys) {
|
||||
result = redisTemplate.delete(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对某个主键对应的值加一,value值必须是全数字的字符串
|
||||
*
|
||||
* @param key the key
|
||||
* @return the long
|
||||
*/
|
||||
public long incr(final String key) {
|
||||
return redisTemplate.opsForValue().increment(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List 引擎
|
||||
*
|
||||
* @return the list operations
|
||||
*/
|
||||
public ListOperations<String, Object> opsForList() {
|
||||
return redisTemplate.opsForList();
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 将一个或多个值 value 插入到列表 key 的表头
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
* @return the long
|
||||
*/
|
||||
public Long leftPush(String key, Object value) {
|
||||
return opsForList().leftPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 移除并返回列表 key 的头元素
|
||||
*
|
||||
* @param key the key
|
||||
* @return the string
|
||||
*/
|
||||
public Object leftPop(String key) {
|
||||
return opsForList().leftPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 :将一个或多个值 value 插入到列表 key 的表尾(最右边)。
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
* @return the long
|
||||
*/
|
||||
public Long in(String key, Object value) {
|
||||
return opsForList().rightPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 移除并返回列表 key 的末尾元素
|
||||
*
|
||||
* @param key the key
|
||||
* @return the string
|
||||
*/
|
||||
public Object rightPop(String key) {
|
||||
return opsForList().rightPop(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 返回列表 key 的长度 ; 如果 key 不存在,则 key 被解释为一个空列表,返回 0 ; 如果 key 不是列表类型,返回一个错误。
|
||||
*
|
||||
* @param key the key
|
||||
* @return the long
|
||||
*/
|
||||
public Long length(String key) {
|
||||
return opsForList().size(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 根据参数 i 的值,移除列表中与参数 value 相等的元素
|
||||
*
|
||||
* @param key the key
|
||||
* @param i the
|
||||
* @param value the value
|
||||
*/
|
||||
public void remove(String key, long i, Object value) {
|
||||
opsForList().remove(key, i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 将列表 key 下标为 index 的元素的值设置为 value
|
||||
*
|
||||
* @param key the key
|
||||
* @param index the index
|
||||
* @param value the value
|
||||
*/
|
||||
public void set(String key, long index, Object value) {
|
||||
opsForList().set(key, index, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
|
||||
*
|
||||
* @param key the key
|
||||
* @param start the start
|
||||
* @param end the end
|
||||
* @return the list
|
||||
*/
|
||||
public List<Object> getList(String key, int start, int end) {
|
||||
return opsForList().range(key, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
|
||||
*
|
||||
* @param key the key
|
||||
* @param start the start
|
||||
* @param end the end
|
||||
* @param valueSerializer 序列化
|
||||
* @return the list
|
||||
*/
|
||||
public List<Object> getList(String key, int start, int end, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
return redisTemplate.execute(connection -> deserializeValues(connection.lRange(rawKey, start, end), valueSerializer), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 批量存储
|
||||
*
|
||||
* @param key the key
|
||||
* @param list the list
|
||||
* @return the long
|
||||
*/
|
||||
public Long leftPushAll(String key, List<String> list) {
|
||||
return opsForList().leftPushAll(key, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 将值 value 插入到列表 key 当中,位于值 index 之前或之后,默认之后。
|
||||
*
|
||||
* @param key the key
|
||||
* @param index the index
|
||||
* @param value the value
|
||||
*/
|
||||
public void insert(String key, long index, Object value) {
|
||||
opsForList().set(key, index, value);
|
||||
}
|
||||
|
||||
private byte[] rawKey(Object key) {
|
||||
Assert.notNull(key, "non null key required");
|
||||
|
||||
if (key instanceof byte[]) {
|
||||
return (byte[]) key;
|
||||
}
|
||||
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>)redisTemplate.getKeySerializer();
|
||||
return redisSerializer.serialize(key);
|
||||
}
|
||||
private byte[] rawValue(Object value, RedisSerializer valueSerializer) {
|
||||
if (value instanceof byte[]) {
|
||||
return (byte[]) value;
|
||||
}
|
||||
|
||||
return valueSerializer.serialize(value);
|
||||
}
|
||||
|
||||
private List deserializeValues(List<byte[]> rawValues, RedisSerializer<Object> valueSerializer) {
|
||||
if (valueSerializer == null) {
|
||||
return rawValues;
|
||||
}
|
||||
return SerializationUtils.deserialize(rawValues, valueSerializer);
|
||||
}
|
||||
|
||||
private Object deserializeValue(byte[] value, RedisSerializer<Object> valueSerializer) {
|
||||
if (valueSerializer == null) {
|
||||
return value;
|
||||
}
|
||||
return valueSerializer.deserialize(value);
|
||||
}
|
||||
}
|
||||
package com.central.common.redis.template;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisClusterNode;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisServerCommands;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Redis Repository
|
||||
* redis 基本操作 可扩展,基本够用了
|
||||
*
|
||||
* @author zlt
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisRepository {
|
||||
/**
|
||||
* Spring Redis Template
|
||||
*/
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
public RedisRepository(RedisTemplate<String, Object> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取链接工厂
|
||||
*/
|
||||
public RedisConnectionFactory getConnectionFactory() {
|
||||
return this.redisTemplate.getConnectionFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 RedisTemplate对象
|
||||
*/
|
||||
public RedisTemplate<String, Object> getRedisTemplate() {
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空DB
|
||||
*
|
||||
* @param node redis 节点
|
||||
*/
|
||||
public void flushDB(RedisClusterNode node) {
|
||||
this.redisTemplate.opsForCluster().flushDb(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到带有 过期时间的 缓存
|
||||
*
|
||||
* @param key redis主键
|
||||
* @param value 值
|
||||
* @param time 过期时间(单位秒)
|
||||
*/
|
||||
public void setExpire(final byte[] key, final byte[] value, final long time) {
|
||||
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
||||
connection.setEx(key, time, value);
|
||||
return 1L;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到带有 过期时间的 缓存
|
||||
*
|
||||
* @param key redis主键
|
||||
* @param value 值
|
||||
* @param time 过期时间
|
||||
* @param timeUnit 过期时间单位
|
||||
*/
|
||||
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit) {
|
||||
redisTemplate.opsForValue().set(key, value, time, timeUnit);
|
||||
}
|
||||
public void setExpire(final String key, final Object value, final long time) {
|
||||
this.setExpire(key, value, time, TimeUnit.SECONDS);
|
||||
}
|
||||
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
byte[] rawValue = rawValue(value, valueSerializer);
|
||||
|
||||
redisTemplate.execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
potentiallyUsePsetEx(connection);
|
||||
return null;
|
||||
}
|
||||
public void potentiallyUsePsetEx(RedisConnection connection) {
|
||||
if (!TimeUnit.MILLISECONDS.equals(timeUnit) || !failsafeInvokePsetEx(connection)) {
|
||||
connection.setEx(rawKey, TimeoutUtils.toSeconds(time, timeUnit), rawValue);
|
||||
}
|
||||
}
|
||||
private boolean failsafeInvokePsetEx(RedisConnection connection) {
|
||||
boolean failed = false;
|
||||
try {
|
||||
connection.pSetEx(rawKey, time, rawValue);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
failed = true;
|
||||
}
|
||||
return !failed;
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
|
||||
*
|
||||
* @param keys redis主键数组
|
||||
* @param values 值数组
|
||||
* @param time 过期时间(单位秒)
|
||||
*/
|
||||
public void setExpire(final String[] keys, final Object[] values, final long time) {
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
redisTemplate.opsForValue().set(keys[i], values[i], time, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
|
||||
*
|
||||
* @param keys the keys
|
||||
* @param values the values
|
||||
*/
|
||||
public void set(final String[] keys, final Object[] values) {
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
redisTemplate.opsForValue().set(keys[i], values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加到缓存
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
public void set(final String key, final Object value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询在以keyPatten的所有 key
|
||||
*
|
||||
* @param keyPatten the key patten
|
||||
* @return the set
|
||||
*/
|
||||
public Set<String> keys(final String keyPatten) {
|
||||
return redisTemplate.keys(keyPatten + "*");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key获取对象
|
||||
*
|
||||
* @param key the key
|
||||
* @return the byte [ ]
|
||||
*/
|
||||
public byte[] get(final byte[] key) {
|
||||
return redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key获取对象
|
||||
*
|
||||
* @param key the key
|
||||
* @return the string
|
||||
*/
|
||||
public Object get(final String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
*获取原来key键对应的值并重新赋新值。
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public String getAndSet(final String key,String value) {
|
||||
String result = null;
|
||||
if (StringUtils.isEmpty(key)){
|
||||
log.error("非法入参");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Object object =redisTemplate.opsForValue().getAndSet(key, value);
|
||||
if (object !=null){
|
||||
result = object.toString();
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("redisTemplate操作异常",e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 根据key获取对象
|
||||
*
|
||||
* @param key the key
|
||||
* @param valueSerializer 序列化
|
||||
* @return the string
|
||||
*/
|
||||
public Object get(final String key, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
return redisTemplate.execute(connection -> deserializeValue(connection.get(rawKey), valueSerializer), true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ops for hash hash operations.
|
||||
*
|
||||
* @return the hash operations
|
||||
*/
|
||||
public HashOperations<String, String, Object> opsForHash() {
|
||||
return redisTemplate.opsForHash();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对HashMap操作
|
||||
*
|
||||
* @param key the key
|
||||
* @param hashKey the hash key
|
||||
* @param hashValue the hash value
|
||||
*/
|
||||
public void putHashValue(String key, String hashKey, Object hashValue) {
|
||||
opsForHash().put(key, hashKey, hashValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单个field对应的值
|
||||
*
|
||||
* @param key the key
|
||||
* @param hashKey the hash key
|
||||
* @return the hash values
|
||||
*/
|
||||
public Object getHashValues(String key, String hashKey) {
|
||||
return opsForHash().get(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key值删除
|
||||
*
|
||||
* @param key the key
|
||||
* @param hashKeys the hash keys
|
||||
*/
|
||||
public void delHashValues(String key, Object... hashKeys) {
|
||||
opsForHash().delete(key, hashKeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* key只匹配map
|
||||
*
|
||||
* @param key the key
|
||||
* @return the hash value
|
||||
*/
|
||||
public Map<String, Object> getHashValue(String key) {
|
||||
return opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加
|
||||
*
|
||||
* @param key the key
|
||||
* @param map the map
|
||||
*/
|
||||
public void putHashValues(String key, Map<String, Object> map) {
|
||||
opsForHash().putAll(key, map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合数量
|
||||
*
|
||||
* @return the long
|
||||
*/
|
||||
public long dbSize() {
|
||||
return redisTemplate.execute(RedisServerCommands::dbSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空redis存储的数据
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
public String flushDB() {
|
||||
return redisTemplate.execute((RedisCallback<String>) connection -> {
|
||||
connection.flushDb();
|
||||
return "ok";
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断某个主键是否存在
|
||||
*
|
||||
* @param key the key
|
||||
* @return the boolean
|
||||
*/
|
||||
public boolean exists(final String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除key
|
||||
*
|
||||
* @param keys the keys
|
||||
* @return the long
|
||||
*/
|
||||
public boolean del(final String... keys) {
|
||||
boolean result = false;
|
||||
for (String key : keys) {
|
||||
result = redisTemplate.delete(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对某个主键对应的值加一,value值必须是全数字的字符串
|
||||
*
|
||||
* @param key the key
|
||||
* @return the long
|
||||
*/
|
||||
public long incr(final String key) {
|
||||
return redisTemplate.opsForValue().increment(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List 引擎
|
||||
*
|
||||
* @return the list operations
|
||||
*/
|
||||
public ListOperations<String, Object> opsForList() {
|
||||
return redisTemplate.opsForList();
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 将一个或多个值 value 插入到列表 key 的表头
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
* @return the long
|
||||
*/
|
||||
public Long leftPush(String key, Object value) {
|
||||
return opsForList().leftPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 移除并返回列表 key 的头元素
|
||||
*
|
||||
* @param key the key
|
||||
* @return the string
|
||||
*/
|
||||
public Object leftPop(String key) {
|
||||
return opsForList().leftPop(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 :将一个或多个值 value 插入到列表 key 的表尾(最右边)。
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
* @return the long
|
||||
*/
|
||||
public Long in(String key, Object value) {
|
||||
return opsForList().rightPush(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 移除并返回列表 key 的末尾元素
|
||||
*
|
||||
* @param key the key
|
||||
* @return the string
|
||||
*/
|
||||
public Object rightPop(String key) {
|
||||
return opsForList().rightPop(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 返回列表 key 的长度 ; 如果 key 不存在,则 key 被解释为一个空列表,返回 0 ; 如果 key 不是列表类型,返回一个错误。
|
||||
*
|
||||
* @param key the key
|
||||
* @return the long
|
||||
*/
|
||||
public Long length(String key) {
|
||||
return opsForList().size(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 根据参数 i 的值,移除列表中与参数 value 相等的元素
|
||||
*
|
||||
* @param key the key
|
||||
* @param i the
|
||||
* @param value the value
|
||||
*/
|
||||
public void remove(String key, long i, Object value) {
|
||||
opsForList().remove(key, i, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 将列表 key 下标为 index 的元素的值设置为 value
|
||||
*
|
||||
* @param key the key
|
||||
* @param index the index
|
||||
* @param value the value
|
||||
*/
|
||||
public void set(String key, long index, Object value) {
|
||||
opsForList().set(key, index, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
|
||||
*
|
||||
* @param key the key
|
||||
* @param start the start
|
||||
* @param end the end
|
||||
* @return the list
|
||||
*/
|
||||
public List<Object> getList(String key, int start, int end) {
|
||||
return opsForList().range(key, start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
|
||||
*
|
||||
* @param key the key
|
||||
* @param start the start
|
||||
* @param end the end
|
||||
* @param valueSerializer 序列化
|
||||
* @return the list
|
||||
*/
|
||||
public List<Object> getList(String key, int start, int end, RedisSerializer<Object> valueSerializer) {
|
||||
byte[] rawKey = rawKey(key);
|
||||
return redisTemplate.execute(connection -> deserializeValues(connection.lRange(rawKey, start, end), valueSerializer), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 批量存储
|
||||
*
|
||||
* @param key the key
|
||||
* @param list the list
|
||||
* @return the long
|
||||
*/
|
||||
public Long leftPushAll(String key, List<String> list) {
|
||||
return opsForList().leftPushAll(key, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* redis List数据结构 : 将值 value 插入到列表 key 当中,位于值 index 之前或之后,默认之后。
|
||||
*
|
||||
* @param key the key
|
||||
* @param index the index
|
||||
* @param value the value
|
||||
*/
|
||||
public void insert(String key, long index, Object value) {
|
||||
opsForList().set(key, index, value);
|
||||
}
|
||||
|
||||
private byte[] rawKey(Object key) {
|
||||
Assert.notNull(key, "non null key required");
|
||||
|
||||
if (key instanceof byte[]) {
|
||||
return (byte[]) key;
|
||||
}
|
||||
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>)redisTemplate.getKeySerializer();
|
||||
return redisSerializer.serialize(key);
|
||||
}
|
||||
private byte[] rawValue(Object value, RedisSerializer valueSerializer) {
|
||||
if (value instanceof byte[]) {
|
||||
return (byte[]) value;
|
||||
}
|
||||
|
||||
return valueSerializer.serialize(value);
|
||||
}
|
||||
|
||||
private List deserializeValues(List<byte[]> rawValues, RedisSerializer<Object> valueSerializer) {
|
||||
if (valueSerializer == null) {
|
||||
return rawValues;
|
||||
}
|
||||
return SerializationUtils.deserialize(rawValues, valueSerializer);
|
||||
}
|
||||
|
||||
private Object deserializeValue(byte[] value, RedisSerializer<Object> valueSerializer) {
|
||||
if (valueSerializer == null) {
|
||||
return value;
|
||||
}
|
||||
return valueSerializer.deserialize(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
public class RedisListenerConfig {
|
||||
@Bean
|
||||
@Primary
|
||||
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
|
||||
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory) {
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.setConnectionFactory(factory);
|
||||
return container;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,9 +54,10 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
|
||||
}
|
||||
String accessValue = expiredKey.substring(expiredKey.indexOf(":") + 1);
|
||||
// 分布式集群部署下防止一个过期被多个服务重复消费
|
||||
String qc = redisRepository.getAndSet("qc:" + accessValue, "1");
|
||||
if (StringUtils.isNotEmpty(qc) && "1".equals(qc)) {
|
||||
log.debug("其他节点已经处理了该数据,次数跳过");
|
||||
String qc = "qc:" + accessValue;
|
||||
String oldLock = redisRepository.getAndSet(qc, "1");
|
||||
if (StringUtils.isNotEmpty(oldLock) && "1".equals(oldLock)) {
|
||||
log.debug("其他节点已经处理了该数据,跳过");
|
||||
return;
|
||||
}
|
||||
byte[] accessBakKey = serializeKey(SecurityConstants.ACCESS_BAK + accessValue);
|
||||
@@ -77,7 +78,7 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
} finally {
|
||||
conn.del();
|
||||
conn.del(oldLock.getBytes());
|
||||
conn.close();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user