优化redis序列化代码
This commit is contained in:
+5
-7
@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,17 +14,14 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
|
|||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2018/7/25 9:36
|
* @date 2018/7/25 9:36
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
|
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
|
||||||
public class AuthRedisTokenStore {
|
public class AuthRedisTokenStore {
|
||||||
@Autowired
|
|
||||||
private RedisConnectionFactory connectionFactory;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private SecurityProperties securityProperties;
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TokenStore tokenStore() {
|
public TokenStore tokenStore(RedisConnectionFactory connectionFactory, SecurityProperties securityProperties) {
|
||||||
return new CustomRedisTokenStore(connectionFactory, securityProperties);
|
return new CustomRedisTokenStore(connectionFactory, securityProperties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-15
@@ -1,11 +1,8 @@
|
|||||||
package com.central.common.redis;
|
package com.central.common.redis;
|
||||||
|
|
||||||
import com.central.common.redis.properties.CacheManagerProperties;
|
import com.central.common.redis.properties.CacheManagerProperties;
|
||||||
import com.central.common.redis.template.RedisRepository;
|
|
||||||
import com.central.common.redis.util.RedisObjectSerializer;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cache.interceptor.KeyGenerator;
|
import org.springframework.cache.interceptor.KeyGenerator;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
@@ -27,6 +24,9 @@ import java.util.Map;
|
|||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2018/11/6 11:02
|
* @date 2018/11/6 11:02
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@EnableConfigurationProperties({RedisProperties.class, CacheManagerProperties.class})
|
@EnableConfigurationProperties({RedisProperties.class, CacheManagerProperties.class})
|
||||||
@EnableCaching
|
@EnableCaching
|
||||||
@@ -34,35 +34,45 @@ public class RedisAutoConfigure {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CacheManagerProperties cacheManagerProperties;
|
private CacheManagerProperties cacheManagerProperties;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisSerializer<String> redisKeySerializer() {
|
||||||
|
return RedisSerializer.string();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisSerializer<Object> redisValueSerializer() {
|
||||||
|
return RedisSerializer.java();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RedisTemplate配置
|
* RedisTemplate配置
|
||||||
* @param factory
|
* @param factory
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory
|
||||||
|
, RedisSerializer<String> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
|
||||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||||
redisTemplate.setConnectionFactory(factory);
|
redisTemplate.setConnectionFactory(factory);
|
||||||
|
|
||||||
RedisSerializer stringSerializer = new StringRedisSerializer();
|
redisTemplate.setDefaultSerializer(redisValueSerializer);
|
||||||
RedisSerializer redisObjectSerializer = new RedisObjectSerializer();
|
redisTemplate.setKeySerializer(redisKeySerializer);
|
||||||
redisTemplate.setKeySerializer(stringSerializer);
|
redisTemplate.setHashKeySerializer(redisKeySerializer);
|
||||||
redisTemplate.setHashKeySerializer(stringSerializer);
|
|
||||||
redisTemplate.setValueSerializer(redisObjectSerializer);
|
|
||||||
redisTemplate.afterPropertiesSet();
|
redisTemplate.afterPropertiesSet();
|
||||||
return redisTemplate;
|
return redisTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(name = "cacheManager")
|
@Bean(name = "cacheManager")
|
||||||
@Primary
|
@Primary
|
||||||
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory
|
||||||
RedisCacheConfiguration difConf = getDefConf().entryTtl(Duration.ofHours(1));
|
, RedisSerializer<String> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
|
||||||
|
RedisCacheConfiguration difConf = getDefConf(redisKeySerializer, redisValueSerializer).entryTtl(Duration.ofHours(1));
|
||||||
|
|
||||||
//自定义的缓存过期时间配置
|
//自定义的缓存过期时间配置
|
||||||
int configSize = cacheManagerProperties.getConfigs() == null ? 0 : cacheManagerProperties.getConfigs().size();
|
int configSize = cacheManagerProperties.getConfigs() == null ? 0 : cacheManagerProperties.getConfigs().size();
|
||||||
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(configSize);
|
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(configSize);
|
||||||
if (configSize > 0) {
|
if (configSize > 0) {
|
||||||
cacheManagerProperties.getConfigs().forEach(e -> {
|
cacheManagerProperties.getConfigs().forEach(e -> {
|
||||||
RedisCacheConfiguration conf = getDefConf().entryTtl(Duration.ofSeconds(e.getSecond()));
|
RedisCacheConfiguration conf = getDefConf(redisKeySerializer, redisValueSerializer).entryTtl(Duration.ofSeconds(e.getSecond()));
|
||||||
redisCacheConfigurationMap.put(e.getKey(), conf);
|
redisCacheConfigurationMap.put(e.getKey(), conf);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -86,11 +96,11 @@ public class RedisAutoConfigure {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private RedisCacheConfiguration getDefConf() {
|
private RedisCacheConfiguration getDefConf(RedisSerializer<String> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
|
||||||
return RedisCacheConfiguration.defaultCacheConfig()
|
return RedisCacheConfiguration.defaultCacheConfig()
|
||||||
.disableCachingNullValues()
|
.disableCachingNullValues()
|
||||||
.computePrefixWith(cacheName -> "cache".concat(":").concat(cacheName).concat(":"))
|
.computePrefixWith(cacheName -> "cache".concat(":").concat(cacheName).concat(":"))
|
||||||
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
|
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisKeySerializer))
|
||||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new RedisObjectSerializer()));
|
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisValueSerializer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-143
@@ -1,7 +1,6 @@
|
|||||||
package com.central.common.redis.template;
|
package com.central.common.redis.template;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
|
||||||
import org.springframework.data.redis.connection.RedisClusterNode;
|
import org.springframework.data.redis.connection.RedisClusterNode;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.connection.RedisServerCommands;
|
import org.springframework.data.redis.connection.RedisServerCommands;
|
||||||
@@ -9,37 +8,21 @@ import org.springframework.data.redis.core.HashOperations;
|
|||||||
import org.springframework.data.redis.core.ListOperations;
|
import org.springframework.data.redis.core.ListOperations;
|
||||||
import org.springframework.data.redis.core.RedisCallback;
|
import org.springframework.data.redis.core.RedisCallback;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis Repository
|
* Redis Repository
|
||||||
* redis 基本操作 可扩展,基本够用了
|
* redis 基本操作 可扩展,基本够用了
|
||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class RedisRepository {
|
public class RedisRepository {
|
||||||
/**
|
|
||||||
* 默认编码
|
|
||||||
*/
|
|
||||||
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* key序列化
|
|
||||||
*/
|
|
||||||
private static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* value 序列化
|
|
||||||
*/
|
|
||||||
private static final JdkSerializationRedisSerializer OBJECT_SERIALIZER = new JdkSerializationRedisSerializer();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring Redis Template
|
* Spring Redis Template
|
||||||
*/
|
*/
|
||||||
@@ -47,8 +30,6 @@ public class RedisRepository {
|
|||||||
|
|
||||||
public RedisRepository(RedisTemplate<String, Object> redisTemplate) {
|
public RedisRepository(RedisTemplate<String, Object> redisTemplate) {
|
||||||
this.redisTemplate = redisTemplate;
|
this.redisTemplate = redisTemplate;
|
||||||
this.redisTemplate.setKeySerializer(STRING_SERIALIZER);
|
|
||||||
this.redisTemplate.setValueSerializer(OBJECT_SERIALIZER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -84,7 +65,6 @@ public class RedisRepository {
|
|||||||
public void setExpire(final byte[] key, final byte[] value, final long time) {
|
public void setExpire(final byte[] key, final byte[] value, final long time) {
|
||||||
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
||||||
connection.setEx(key, time, value);
|
connection.setEx(key, time, value);
|
||||||
log.debug("[redisTemplate redis]放入 缓存 url:{} ========缓存时间为{}秒", key, time);
|
|
||||||
return 1L;
|
return 1L;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -97,13 +77,7 @@ public class RedisRepository {
|
|||||||
* @param time 过期时间(单位秒)
|
* @param time 过期时间(单位秒)
|
||||||
*/
|
*/
|
||||||
public void setExpire(final String key, final Object value, final long time) {
|
public void setExpire(final String key, final Object value, final long time) {
|
||||||
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||||
RedisSerializer<String> serializer = getRedisSerializer();
|
|
||||||
byte[] keys = serializer.serialize(key);
|
|
||||||
byte[] values = OBJECT_SERIALIZER.serialize(value);
|
|
||||||
connection.setEx(keys, time, values);
|
|
||||||
return 1L;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,15 +88,9 @@ public class RedisRepository {
|
|||||||
* @param time 过期时间(单位秒)
|
* @param time 过期时间(单位秒)
|
||||||
*/
|
*/
|
||||||
public void setExpire(final String[] keys, final Object[] values, final long time) {
|
public void setExpire(final String[] keys, final Object[] values, final long time) {
|
||||||
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
for (int i = 0; i < keys.length; i++) {
|
||||||
RedisSerializer<String> serializer = getRedisSerializer();
|
redisTemplate.opsForValue().set(keys[i], values[i], time, TimeUnit.SECONDS);
|
||||||
for (int i = 0; i < keys.length; i++) {
|
}
|
||||||
byte[] bKeys = serializer.serialize(keys[i]);
|
|
||||||
byte[] bValues = OBJECT_SERIALIZER.serialize(values[i]);
|
|
||||||
connection.setEx(bKeys, time, bValues);
|
|
||||||
}
|
|
||||||
return 1L;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -133,15 +101,9 @@ public class RedisRepository {
|
|||||||
* @param values the values
|
* @param values the values
|
||||||
*/
|
*/
|
||||||
public void set(final String[] keys, final Object[] values) {
|
public void set(final String[] keys, final Object[] values) {
|
||||||
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
for (int i = 0; i < keys.length; i++) {
|
||||||
RedisSerializer<String> serializer = getRedisSerializer();
|
redisTemplate.opsForValue().set(keys[i], values[i]);
|
||||||
for (int i = 0; i < keys.length; i++) {
|
}
|
||||||
byte[] bKeys = serializer.serialize(keys[i]);
|
|
||||||
byte[] bValues = OBJECT_SERIALIZER.serialize(values[i]);
|
|
||||||
connection.set(bKeys, bValues);
|
|
||||||
}
|
|
||||||
return 1L;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -152,39 +114,9 @@ public class RedisRepository {
|
|||||||
* @param value the value
|
* @param value the value
|
||||||
*/
|
*/
|
||||||
public void set(final String key, final Object value) {
|
public void set(final String key, final Object value) {
|
||||||
redisTemplate.execute((RedisCallback<Long>) connection -> {
|
redisTemplate.opsForValue().set(key, value);
|
||||||
RedisSerializer<String> serializer = getRedisSerializer();
|
|
||||||
byte[] keys = serializer.serialize(key);
|
|
||||||
byte[] values = OBJECT_SERIALIZER.serialize(value);
|
|
||||||
connection.set(keys, values);
|
|
||||||
log.debug("[redisTemplate redis]放入 缓存 url:{}", key);
|
|
||||||
return 1L;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询在这个时间段内即将过期的key
|
|
||||||
*
|
|
||||||
* @param key the key
|
|
||||||
* @param time the time
|
|
||||||
* @return the list
|
|
||||||
*/
|
|
||||||
public List<String> willExpire(final String key, final long time) {
|
|
||||||
final List<String> keysList = new ArrayList<>();
|
|
||||||
redisTemplate.execute((RedisCallback<List<String>>) connection -> {
|
|
||||||
Set<String> keys = redisTemplate.keys(key + "*");
|
|
||||||
for (String key1 : keys) {
|
|
||||||
Long ttl = connection.ttl(key1.getBytes(DEFAULT_CHARSET));
|
|
||||||
if (0 <= ttl && ttl <= 2 * time) {
|
|
||||||
keysList.add(key1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return keysList;
|
|
||||||
});
|
|
||||||
return keysList;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询在以keyPatten的所有 key
|
* 查询在以keyPatten的所有 key
|
||||||
*
|
*
|
||||||
@@ -192,7 +124,7 @@ public class RedisRepository {
|
|||||||
* @return the set
|
* @return the set
|
||||||
*/
|
*/
|
||||||
public Set<String> keys(final String keyPatten) {
|
public Set<String> keys(final String keyPatten) {
|
||||||
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> redisTemplate.keys(keyPatten + "*"));
|
return redisTemplate.keys(keyPatten + "*");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -202,9 +134,7 @@ public class RedisRepository {
|
|||||||
* @return the byte [ ]
|
* @return the byte [ ]
|
||||||
*/
|
*/
|
||||||
public byte[] get(final byte[] key) {
|
public byte[] get(final byte[] key) {
|
||||||
byte[] result = redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key));
|
return redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key));
|
||||||
log.debug("[redisTemplate redis]取出 缓存 url:{} ", key);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -214,39 +144,7 @@ public class RedisRepository {
|
|||||||
* @return the string
|
* @return the string
|
||||||
*/
|
*/
|
||||||
public Object get(final String key) {
|
public Object get(final String key) {
|
||||||
Object resultStr = redisTemplate.execute((RedisCallback<Object>) connection -> {
|
return redisTemplate.opsForValue().get(key);
|
||||||
RedisSerializer<String> serializer = getRedisSerializer();
|
|
||||||
byte[] keys = serializer.serialize(key);
|
|
||||||
byte[] values = connection.get(keys);
|
|
||||||
return OBJECT_SERIALIZER.deserialize(values);
|
|
||||||
});
|
|
||||||
log.debug("[redisTemplate redis]取出 缓存 url:{} ", key);
|
|
||||||
return resultStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据key获取对象
|
|
||||||
*
|
|
||||||
* @param keyPatten the key patten
|
|
||||||
* @return the keys values
|
|
||||||
*/
|
|
||||||
public Map<String, Object> getKeysValues(final String keyPatten) {
|
|
||||||
log.debug("[redisTemplate redis] getValues() patten={} ", keyPatten);
|
|
||||||
return redisTemplate.execute((RedisCallback<Map<String, Object>>) connection -> {
|
|
||||||
RedisSerializer<String> serializer = getRedisSerializer();
|
|
||||||
Map<String, Object> maps = new HashMap<>(16);
|
|
||||||
Set<String> keys = redisTemplate.keys(keyPatten + "*");
|
|
||||||
if (CollectionUtils.isNotEmpty(keys)) {
|
|
||||||
for (String key : keys) {
|
|
||||||
byte[] bKeys = serializer.serialize(key);
|
|
||||||
byte[] bValues = connection.get(bKeys);
|
|
||||||
Object value = OBJECT_SERIALIZER.deserialize(bValues);
|
|
||||||
maps.put(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return maps;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -266,7 +164,6 @@ public class RedisRepository {
|
|||||||
* @param hashValue the hash value
|
* @param hashValue the hash value
|
||||||
*/
|
*/
|
||||||
public void putHashValue(String key, String hashKey, Object hashValue) {
|
public void putHashValue(String key, String hashKey, Object hashValue) {
|
||||||
log.debug("[redisTemplate redis] putHashValue() key={},hashKey={},hashValue={} ", key, hashKey, hashValue);
|
|
||||||
opsForHash().put(key, hashKey, hashValue);
|
opsForHash().put(key, hashKey, hashValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +175,6 @@ public class RedisRepository {
|
|||||||
* @return the hash values
|
* @return the hash values
|
||||||
*/
|
*/
|
||||||
public Object getHashValues(String key, String hashKey) {
|
public Object getHashValues(String key, String hashKey) {
|
||||||
log.debug("[redisTemplate redis] getHashValues() key={},hashKey={}", key, hashKey);
|
|
||||||
return opsForHash().get(key, hashKey);
|
return opsForHash().get(key, hashKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,7 +185,6 @@ public class RedisRepository {
|
|||||||
* @param hashKeys the hash keys
|
* @param hashKeys the hash keys
|
||||||
*/
|
*/
|
||||||
public void delHashValues(String key, Object... hashKeys) {
|
public void delHashValues(String key, Object... hashKeys) {
|
||||||
log.debug("[redisTemplate redis] delHashValues() key={}", key);
|
|
||||||
opsForHash().delete(key, hashKeys);
|
opsForHash().delete(key, hashKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,7 +195,6 @@ public class RedisRepository {
|
|||||||
* @return the hash value
|
* @return the hash value
|
||||||
*/
|
*/
|
||||||
public Map<String, Object> getHashValue(String key) {
|
public Map<String, Object> getHashValue(String key) {
|
||||||
log.debug("[redisTemplate redis] getHashValue() key={}", key);
|
|
||||||
return opsForHash().entries(key);
|
return opsForHash().entries(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,7 +236,7 @@ public class RedisRepository {
|
|||||||
* @return the boolean
|
* @return the boolean
|
||||||
*/
|
*/
|
||||||
public boolean exists(final String key) {
|
public boolean exists(final String key) {
|
||||||
return redisTemplate.execute((RedisCallback<Boolean>) connection -> connection.exists(key.getBytes(DEFAULT_CHARSET)));
|
return redisTemplate.hasKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -352,23 +246,12 @@ public class RedisRepository {
|
|||||||
* @param keys the keys
|
* @param keys the keys
|
||||||
* @return the long
|
* @return the long
|
||||||
*/
|
*/
|
||||||
public long del(final String... keys) {
|
public boolean del(final String... keys) {
|
||||||
return redisTemplate.execute((RedisCallback<Long>) connection -> {
|
boolean result = false;
|
||||||
long result = 0;
|
for (String key : keys) {
|
||||||
for (String key : keys) {
|
result = redisTemplate.delete(key);
|
||||||
result = connection.del(key.getBytes(DEFAULT_CHARSET));
|
}
|
||||||
}
|
return result;
|
||||||
return result;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取 RedisSerializer
|
|
||||||
*
|
|
||||||
* @return the redis serializer
|
|
||||||
*/
|
|
||||||
protected RedisSerializer<String> getRedisSerializer() {
|
|
||||||
return redisTemplate.getStringSerializer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -378,10 +261,7 @@ public class RedisRepository {
|
|||||||
* @return the long
|
* @return the long
|
||||||
*/
|
*/
|
||||||
public long incr(final String key) {
|
public long incr(final String key) {
|
||||||
return redisTemplate.execute((RedisCallback<Long>) connection -> {
|
return redisTemplate.opsForValue().increment(key);
|
||||||
RedisSerializer<String> redisSerializer = getRedisSerializer();
|
|
||||||
return connection.incr(redisSerializer.serialize(key));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user