优化rediskey前缀
This commit is contained in:
+22
-27
@@ -2,8 +2,8 @@ package xtools.boot.cache.redis.base;
|
|||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.Getter;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
||||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import xtools.core.ArrUtils;
|
import xtools.core.ArrUtils;
|
||||||
@@ -32,6 +32,7 @@ import static xtools.base.config.Constants.CP_NUM1;
|
|||||||
* @version : 5.0.0
|
* @version : 5.0.0
|
||||||
* @date : 2026/01/01 09:30
|
* @date : 2026/01/01 09:30
|
||||||
*/
|
*/
|
||||||
|
@Getter
|
||||||
@Component
|
@Component
|
||||||
public class RedisService {
|
public class RedisService {
|
||||||
|
|
||||||
@@ -41,12 +42,6 @@ public class RedisService {
|
|||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate<String, Object> redisTemplate;
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
/**
|
|
||||||
* StringRedisTemplate
|
|
||||||
**/
|
|
||||||
@Resource
|
|
||||||
private StringRedisTemplate stringRedisTemplate;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取组合 Key
|
* 获取组合 Key
|
||||||
*
|
*
|
||||||
@@ -71,7 +66,7 @@ public class RedisService {
|
|||||||
* @return 递增后的值
|
* @return 递增后的值
|
||||||
*/
|
*/
|
||||||
public Long incr(String key) {
|
public Long incr(String key) {
|
||||||
return stringRedisTemplate.opsForValue().increment(key);
|
return redisTemplate.opsForValue().increment(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -123,9 +118,9 @@ public class RedisService {
|
|||||||
}
|
}
|
||||||
// 判断是否有过期时间
|
// 判断是否有过期时间
|
||||||
if (timeout == null || timeout <= 0) {
|
if (timeout == null || timeout <= 0) {
|
||||||
stringRedisTemplate.opsForValue().set(key, serializeData);
|
redisTemplate.opsForValue().set(key, serializeData);
|
||||||
} else {
|
} else {
|
||||||
stringRedisTemplate.opsForValue().set(key, serializeData, Duration.of(timeout, unit.toChronoUnit()));
|
redisTemplate.opsForValue().set(key, serializeData, Duration.of(timeout, unit.toChronoUnit()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,9 +176,9 @@ public class RedisService {
|
|||||||
}
|
}
|
||||||
// 判断是否有过期时间
|
// 判断是否有过期时间
|
||||||
if (timeout == null || timeout <= 0) {
|
if (timeout == null || timeout <= 0) {
|
||||||
return stringRedisTemplate.opsForValue().setIfAbsent(key, serializeData);
|
return redisTemplate.opsForValue().setIfAbsent(key, serializeData);
|
||||||
} else {
|
} else {
|
||||||
return stringRedisTemplate.opsForValue().setIfAbsent(key, serializeData, Duration.of(timeout, unit.toChronoUnit()));
|
return redisTemplate.opsForValue().setIfAbsent(key, serializeData, Duration.of(timeout, unit.toChronoUnit()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,8 +196,8 @@ public class RedisService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// 从缓存获取数据
|
// 从缓存获取数据
|
||||||
String data = stringRedisTemplate.opsForValue().get(key);
|
Object data = redisTemplate.opsForValue().get(key);
|
||||||
if (StringUtils.isEmpty(data)) {
|
if (data == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return JSON.to(clazz, data);
|
return JSON.to(clazz, data);
|
||||||
@@ -219,7 +214,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(key)) {
|
if (StringUtils.isEmpty(key)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return stringRedisTemplate.delete(key);
|
return redisTemplate.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -233,7 +228,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(oldKey) || StringUtils.isEmpty(newKey)) {
|
if (StringUtils.isEmpty(oldKey) || StringUtils.isEmpty(newKey)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stringRedisTemplate.rename(oldKey, newKey);
|
redisTemplate.rename(oldKey, newKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -248,7 +243,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(key) || timeout <= 0) {
|
if (StringUtils.isEmpty(key) || timeout <= 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return stringRedisTemplate.expire(key, Duration.of(timeout, TimeUnit.SECONDS.toChronoUnit()));
|
return redisTemplate.expire(key, Duration.of(timeout, TimeUnit.SECONDS.toChronoUnit()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -262,7 +257,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(key)) {
|
if (StringUtils.isEmpty(key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return stringRedisTemplate.getExpire(key);
|
return redisTemplate.getExpire(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -276,7 +271,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(key)) {
|
if (StringUtils.isEmpty(key)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return stringRedisTemplate.persist(key);
|
return redisTemplate.persist(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -290,7 +285,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(pattern)) {
|
if (StringUtils.isEmpty(pattern)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return stringRedisTemplate.keys(pattern);
|
return redisTemplate.keys(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -339,7 +334,7 @@ public class RedisService {
|
|||||||
if (CollectionUtils.isEmpty(map)) {
|
if (CollectionUtils.isEmpty(map)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
stringRedisTemplate.opsForHash().putAll(key, map);
|
redisTemplate.opsForHash().putAll(key, map);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,7 +361,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(serializeData)) {
|
if (StringUtils.isEmpty(serializeData)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
stringRedisTemplate.opsForHash().put(key, hashKey, serializeData);
|
redisTemplate.opsForHash().put(key, hashKey, serializeData);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,7 +393,7 @@ public class RedisService {
|
|||||||
"redis.call('HSET', KEYS[1], ARGV[1], ARGV[2]) " +
|
"redis.call('HSET', KEYS[1], ARGV[1], ARGV[2]) " +
|
||||||
"redis.call('HEXPIRE', KEYS[1], ARGV[3], 'FIELDS', 1, ARGV[1]) " +
|
"redis.call('HEXPIRE', KEYS[1], ARGV[3], 'FIELDS', 1, ARGV[1]) " +
|
||||||
"return 1";
|
"return 1";
|
||||||
Long result = stringRedisTemplate.execute(
|
Long result = redisTemplate.execute(
|
||||||
new DefaultRedisScript<>(script, Long.class),
|
new DefaultRedisScript<>(script, Long.class),
|
||||||
Collections.singletonList(key),
|
Collections.singletonList(key),
|
||||||
hashKey, serializeData, String.valueOf(expireTime)
|
hashKey, serializeData, String.valueOf(expireTime)
|
||||||
@@ -419,7 +414,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(key)) {
|
if (StringUtils.isEmpty(key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Map<Object, Object> data = stringRedisTemplate.opsForHash().entries(key);
|
Map<Object, Object> data = redisTemplate.opsForHash().entries(key);
|
||||||
if (CollectionUtils.isEmpty(data)) {
|
if (CollectionUtils.isEmpty(data)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -440,7 +435,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isEmpty(key) || Objects.isNull(hashKey)) {
|
if (StringUtils.isEmpty(key) || Objects.isNull(hashKey)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Object data = stringRedisTemplate.opsForHash().get(key, hashKey);
|
Object data = redisTemplate.opsForHash().get(key, hashKey);
|
||||||
return JSON.to(clazz, data);
|
return JSON.to(clazz, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -455,7 +450,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isBlank(key) || ArrUtils.isEmpty(hashKeys)) {
|
if (StringUtils.isBlank(key) || ArrUtils.isEmpty(hashKeys)) {
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
return stringRedisTemplate.opsForHash().delete(key, hashKeys);
|
return redisTemplate.opsForHash().delete(key, hashKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -469,7 +464,7 @@ public class RedisService {
|
|||||||
if (StringUtils.isBlank(key) || Objects.isNull(hashKey)) {
|
if (StringUtils.isBlank(key) || Objects.isNull(hashKey)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return stringRedisTemplate.opsForHash().hasKey(key, hashKey);
|
return redisTemplate.opsForHash().hasKey(key, hashKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
package xtools.boot.cache.redis.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Title : RedisConfig</p>
|
||||||
|
* <p>Description : RedisConfig</p>
|
||||||
|
* <p>DevelopTools : Idea_x64_v2026.2.0.1</p>
|
||||||
|
* <p>DevelopSystem : macOS Tahoe 26.6</p>
|
||||||
|
* <p>Company : org.xujun</p>
|
||||||
|
*
|
||||||
|
* @author : XuJun
|
||||||
|
* @version : 1.0.0
|
||||||
|
* @date : 2026/8/14 12:30
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "redis")
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redis Key前缀
|
||||||
|
*/
|
||||||
|
private String keyPrev;
|
||||||
|
|
||||||
|
}
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
package xtools.boot.cache.redis.config;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
|
import xtools.boot.cache.redis.serializer.RedisKeySerializer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Title : RedisConfig</p>
|
||||||
|
* <p>Description : RedisConfig</p>
|
||||||
|
* <p>DevelopTools : Idea_x64_v2026.2.0.1</p>
|
||||||
|
* <p>DevelopSystem : macOS Tahoe 26.6</p>
|
||||||
|
* <p>Company : org.xujun</p>
|
||||||
|
*
|
||||||
|
* @author : XuJun
|
||||||
|
* @version : 1.0.0
|
||||||
|
* @date : 2026/8/14 10:49
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class RedisTemplateConfig {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisConfig redisConfig;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate<Object, Object> redisTemplate;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void initRedisTemplate() {
|
||||||
|
// 创建 RedisKeySerializer
|
||||||
|
RedisKeySerializer keySerializer = new RedisKeySerializer(redisConfig.getKeyPrev());
|
||||||
|
// 创建 RedisValueSerializer
|
||||||
|
RedisSerializer<String> valueSerializer = RedisSerializer.string();
|
||||||
|
// key序列化器
|
||||||
|
redisTemplate.setKeySerializer(keySerializer);
|
||||||
|
redisTemplate.setHashKeySerializer(keySerializer);
|
||||||
|
// value序列化器
|
||||||
|
redisTemplate.setValueSerializer(valueSerializer);
|
||||||
|
redisTemplate.setHashValueSerializer(valueSerializer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+82
@@ -0,0 +1,82 @@
|
|||||||
|
package xtools.boot.cache.redis.serializer;
|
||||||
|
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
import xtools.core.StringUtils;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static xtools.base.config.Constants.CP_COLON;
|
||||||
|
import static xtools.base.config.Constants.CP_EMPTY;
|
||||||
|
import static xtools.base.config.Constants.CP_NUM0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Title : RedisKeySerializer</p>
|
||||||
|
* <p>Description : RedisKeySerializer</p>
|
||||||
|
* <p>DevelopTools : Idea_x64_v2026.2.0.1</p>
|
||||||
|
* <p>DevelopSystem : macOS Tahoe 26.6</p>
|
||||||
|
* <p>Company : org.xujun</p>
|
||||||
|
*
|
||||||
|
* @author : XuJun
|
||||||
|
* @version : 1.0.0
|
||||||
|
* @date : 2026/8/14 10:50
|
||||||
|
*/
|
||||||
|
public class RedisKeySerializer extends StringRedisSerializer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key前缀
|
||||||
|
*/
|
||||||
|
private final String keyPrev;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key前缀长度
|
||||||
|
*/
|
||||||
|
private final int keyPrevLength;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
*
|
||||||
|
* @param keyPrev key前缀
|
||||||
|
*/
|
||||||
|
public RedisKeySerializer(String keyPrev) {
|
||||||
|
String key = CP_EMPTY;
|
||||||
|
if (StringUtils.isNotEmpty(keyPrev)) {
|
||||||
|
key = keyPrev + CP_COLON;
|
||||||
|
}
|
||||||
|
this.keyPrev = key;
|
||||||
|
this.keyPrevLength = this.keyPrev.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重写序列化方法
|
||||||
|
*
|
||||||
|
* @param key key
|
||||||
|
* @return byte[]
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public byte[] serialize(String key) {
|
||||||
|
if (StringUtils.isEmpty(key)) {
|
||||||
|
return new byte[CP_NUM0];
|
||||||
|
}
|
||||||
|
return super.serialize(this.keyPrev + key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重写反序列化方法
|
||||||
|
*
|
||||||
|
* @param bytes bytes
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String deserialize(byte[] bytes) {
|
||||||
|
if (bytes == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String key = new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
if (keyPrevLength == CP_NUM0) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return key.startsWith(this.keyPrev) ? key.substring(this.keyPrevLength) : key;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user