From 94d195ae53f428f66e477aea4ef20764cee4b5d5 Mon Sep 17 00:00:00 2001 From: xujun Date: Fri, 14 Aug 2026 15:45:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96rediskey=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../boot/cache/redis/base/RedisService.java | 49 +++++------ .../boot/cache/redis/config/RedisConfig.java | 28 +++++++ .../redis/config/RedisTemplateConfig.java | 44 ++++++++++ .../redis/serializer/RedisKeySerializer.java | 82 +++++++++++++++++++ 4 files changed, 176 insertions(+), 27 deletions(-) create mode 100644 xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisConfig.java create mode 100644 xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisTemplateConfig.java create mode 100644 xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/serializer/RedisKeySerializer.java diff --git a/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/base/RedisService.java b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/base/RedisService.java index 7ffc71d..ce465eb 100644 --- a/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/base/RedisService.java +++ b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/base/RedisService.java @@ -2,8 +2,8 @@ package xtools.boot.cache.redis.base; import com.alibaba.fastjson2.JSON; import jakarta.annotation.Resource; +import lombok.Getter; 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.stereotype.Component; import xtools.core.ArrUtils; @@ -32,6 +32,7 @@ import static xtools.base.config.Constants.CP_NUM1; * @version : 5.0.0 * @date : 2026/01/01 09:30 */ +@Getter @Component public class RedisService { @@ -41,12 +42,6 @@ public class RedisService { @Resource private RedisTemplate redisTemplate; - /** - * StringRedisTemplate - **/ - @Resource - private StringRedisTemplate stringRedisTemplate; - /** * 获取组合 Key * @@ -71,7 +66,7 @@ public class RedisService { * @return 递增后的值 */ 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) { - stringRedisTemplate.opsForValue().set(key, serializeData); + redisTemplate.opsForValue().set(key, serializeData); } 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) { - return stringRedisTemplate.opsForValue().setIfAbsent(key, serializeData); + return redisTemplate.opsForValue().setIfAbsent(key, serializeData); } 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; } // 从缓存获取数据 - String data = stringRedisTemplate.opsForValue().get(key); - if (StringUtils.isEmpty(data)) { + Object data = redisTemplate.opsForValue().get(key); + if (data == null) { return null; } return JSON.to(clazz, data); @@ -219,7 +214,7 @@ public class RedisService { if (StringUtils.isEmpty(key)) { return false; } - return stringRedisTemplate.delete(key); + return redisTemplate.delete(key); } /** @@ -233,7 +228,7 @@ public class RedisService { if (StringUtils.isEmpty(oldKey) || StringUtils.isEmpty(newKey)) { return; } - stringRedisTemplate.rename(oldKey, newKey); + redisTemplate.rename(oldKey, newKey); } /** @@ -248,7 +243,7 @@ public class RedisService { if (StringUtils.isEmpty(key) || timeout <= 0) { 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)) { return null; } - return stringRedisTemplate.getExpire(key); + return redisTemplate.getExpire(key); } /** @@ -276,7 +271,7 @@ public class RedisService { if (StringUtils.isEmpty(key)) { return false; } - return stringRedisTemplate.persist(key); + return redisTemplate.persist(key); } /** @@ -290,7 +285,7 @@ public class RedisService { if (StringUtils.isEmpty(pattern)) { return null; } - return stringRedisTemplate.keys(pattern); + return redisTemplate.keys(pattern); } /** @@ -339,7 +334,7 @@ public class RedisService { if (CollectionUtils.isEmpty(map)) { return false; } - stringRedisTemplate.opsForHash().putAll(key, map); + redisTemplate.opsForHash().putAll(key, map); return true; } @@ -366,7 +361,7 @@ public class RedisService { if (StringUtils.isEmpty(serializeData)) { return false; } - stringRedisTemplate.opsForHash().put(key, hashKey, serializeData); + redisTemplate.opsForHash().put(key, hashKey, serializeData); return true; } @@ -398,7 +393,7 @@ public class RedisService { "redis.call('HSET', KEYS[1], ARGV[1], ARGV[2]) " + "redis.call('HEXPIRE', KEYS[1], ARGV[3], 'FIELDS', 1, ARGV[1]) " + "return 1"; - Long result = stringRedisTemplate.execute( + Long result = redisTemplate.execute( new DefaultRedisScript<>(script, Long.class), Collections.singletonList(key), hashKey, serializeData, String.valueOf(expireTime) @@ -419,7 +414,7 @@ public class RedisService { if (StringUtils.isEmpty(key)) { return null; } - Map data = stringRedisTemplate.opsForHash().entries(key); + Map data = redisTemplate.opsForHash().entries(key); if (CollectionUtils.isEmpty(data)) { return null; } @@ -440,7 +435,7 @@ public class RedisService { if (StringUtils.isEmpty(key) || Objects.isNull(hashKey)) { return null; } - Object data = stringRedisTemplate.opsForHash().get(key, hashKey); + Object data = redisTemplate.opsForHash().get(key, hashKey); return JSON.to(clazz, data); } @@ -455,7 +450,7 @@ public class RedisService { if (StringUtils.isBlank(key) || ArrUtils.isEmpty(hashKeys)) { 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)) { return false; } - return stringRedisTemplate.opsForHash().hasKey(key, hashKey); + return redisTemplate.opsForHash().hasKey(key, hashKey); } } diff --git a/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisConfig.java b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisConfig.java new file mode 100644 index 0000000..267d8e9 --- /dev/null +++ b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisConfig.java @@ -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; + +/** + *

Title : RedisConfig

+ *

Description : RedisConfig

+ *

DevelopTools : Idea_x64_v2026.2.0.1

+ *

DevelopSystem : macOS Tahoe 26.6

+ *

Company : org.xujun

+ * + * @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; + +} diff --git a/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisTemplateConfig.java b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisTemplateConfig.java new file mode 100644 index 0000000..4dd115c --- /dev/null +++ b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/config/RedisTemplateConfig.java @@ -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; + +/** + *

Title : RedisConfig

+ *

Description : RedisConfig

+ *

DevelopTools : Idea_x64_v2026.2.0.1

+ *

DevelopSystem : macOS Tahoe 26.6

+ *

Company : org.xujun

+ * + * @author : XuJun + * @version : 1.0.0 + * @date : 2026/8/14 10:49 + */ +@Configuration +public class RedisTemplateConfig { + + @Resource + private RedisConfig redisConfig; + + @Resource + private RedisTemplate redisTemplate; + + @PostConstruct + public void initRedisTemplate() { + // 创建 RedisKeySerializer + RedisKeySerializer keySerializer = new RedisKeySerializer(redisConfig.getKeyPrev()); + // 创建 RedisValueSerializer + RedisSerializer valueSerializer = RedisSerializer.string(); + // key序列化器 + redisTemplate.setKeySerializer(keySerializer); + redisTemplate.setHashKeySerializer(keySerializer); + // value序列化器 + redisTemplate.setValueSerializer(valueSerializer); + redisTemplate.setHashValueSerializer(valueSerializer); + } + +} diff --git a/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/serializer/RedisKeySerializer.java b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/serializer/RedisKeySerializer.java new file mode 100644 index 0000000..f0c6771 --- /dev/null +++ b/xtools-boot-cache/xtools-boot-cache-redis/src/main/java/xtools/boot/cache/redis/serializer/RedisKeySerializer.java @@ -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; + +/** + *

Title : RedisKeySerializer

+ *

Description : RedisKeySerializer

+ *

DevelopTools : Idea_x64_v2026.2.0.1

+ *

DevelopSystem : macOS Tahoe 26.6

+ *

Company : org.xujun

+ * + * @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; + } + + +} \ No newline at end of file