From 00efa97eefb99fce7a0f903eeddd281a1e318fcd Mon Sep 17 00:00:00 2001 From: PIG AI Date: Fri, 4 Jul 2025 12:24:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(cache):=20=E5=9C=A8RedisUtils=E4=B8=AD?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0increment=E6=96=B9=E6=B3=95=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=AA=8C=E8=AF=81=E7=A0=81=E7=BC=93=E5=AD=98=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增RedisUtils.increment方法用于原子递增操作 - 重构CaptchaCacheServiceProvider使用RedisUtils替代直接stringRedisTemplate调用 - 统一Redis缓存操作工具类方法调用方式 --- .../auth/endpoint/CaptchaCacheServiceProvider.java | 2 +- .../pigx/common/data/cache/RedisUtils.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/CaptchaCacheServiceProvider.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/CaptchaCacheServiceProvider.java index 9f35ddc22..1a9945e34 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/CaptchaCacheServiceProvider.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/CaptchaCacheServiceProvider.java @@ -40,7 +40,7 @@ public class CaptchaCacheServiceProvider implements CaptchaCacheService { @Override public Long increment(String key, long val) { - return stringRedisTemplate.opsForValue().increment(key, val); + return RedisUtils.increment(key, val); } } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisUtils.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisUtils.java index 62f28f19f..a8079e24b 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisUtils.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/cache/RedisUtils.java @@ -270,6 +270,20 @@ public class RedisUtils { return (T) redisTemplate.execute(callback); } + /** + * 递增操作 + * + * @param key 键 + * @param val 增加的值 + * @return 递增后的值 + */ + public Long increment(String key, long val) { + RedisTemplate redisTemplate = SpringContextHolder.getBean(RedisTemplate.class); + return Optional.ofNullable(redisTemplate) + .map(template -> template.opsForValue().increment(key, val)) + .orElse(null); + } + // ================================Map================================= /**