From 6ba6b188497f2af6c7dfb477b7b81ee4334b6f50 Mon Sep 17 00:00:00 2001 From: CoderKK Date: Mon, 11 Aug 2025 09:48:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor(sql):=20=E4=BF=AE=E6=94=B9=20smart?= =?UTF-8?q?=5Fadmin=5Fv3.sql=20=E4=B8=AD=20response=20=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 `response` 字段的注释从 "请求参数" 修改为 "返回值" --- sql/smart_admin_v3.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/smart_admin_v3.sql b/sql/smart_admin_v3.sql index 5838fda9..61abaa46 100644 --- a/sql/smart_admin_v3.sql +++ b/sql/smart_admin_v3.sql @@ -924,7 +924,7 @@ CREATE TABLE `t_operate_log` ( `url` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '请求路径', `method` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '请求方法', `param` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '请求参数', - `response` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '请求参数', + `response` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '返回值', `ip` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '请求ip', `ip_region` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '请求ip地区', `user_agent` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '请求user-agent', From 74aa2da89b81240f6d937db12c8f3a880b13e591 Mon Sep 17 00:00:00 2001 From: 192890 Date: Tue, 12 Aug 2025 06:31:49 +0000 Subject: [PATCH 2/3] =?UTF-8?q?update=20smart-admin-web-javascript/src/vie?= =?UTF-8?q?ws/system/home/index.vue.=20=E4=BF=AE=E6=94=B9=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=9A=E8=AE=A2=E6=AD=A3=E2=80=9C=E5=BE=85=E5=8A=9E?= =?UTF-8?q?=E2=80=9D=E3=80=81=E2=80=9C=E6=9B=B4=E6=96=B0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E2=80=9D=E7=9A=84=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 192890 --- smart-admin-web-javascript/src/views/system/home/index.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smart-admin-web-javascript/src/views/system/home/index.vue b/smart-admin-web-javascript/src/views/system/home/index.vue index 9847a1b4..938aa581 100644 --- a/smart-admin-web-javascript/src/views/system/home/index.vue +++ b/smart-admin-web-javascript/src/views/system/home/index.vue @@ -54,11 +54,11 @@ - + - + From 2eb3742063fd62d82dea75ab52780e92e964a245 Mon Sep 17 00:00:00 2001 From: CoderKK Date: Fri, 15 Aug 2025 13:45:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat(cache):=20=E5=AE=9E=E7=8E=B0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=20Spring=20Cache=20=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=99=A8=E6=94=AF=E6=8C=81=20TTL-=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20CustomRedisCacheManager=20=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E8=A7=A3=E6=9E=90=20cacheName=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=20TTL=20=E8=AE=BE=E7=BD=AE=20-=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20CacheConfig=EF=BC=8C=E4=BD=BF=E7=94=A8=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89=E7=BC=93=E5=AD=98=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=99=A8=20-=20=E6=9B=B4=E6=96=B0=20RedisCacheService?= =?UTF-8?q?Impl=EF=BC=8C=E4=BD=BF=E7=94=A8=20StandardCharsets.UTF=5F8=20?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=20=E9=AD=94=E6=B3=95=E5=80=BC"utf-8"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lab1024/sa/base/config/CacheConfig.java | 28 +++- .../support/cache/RedisCacheServiceImpl.java | 3 +- .../manager/CustomRedisCacheManager.java | 144 ++++++++++++++++++ 3 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/manager/CustomRedisCacheManager.java diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/config/CacheConfig.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/config/CacheConfig.java index 902d5cf0..0ae5ea6a 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/config/CacheConfig.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/config/CacheConfig.java @@ -5,10 +5,13 @@ import jakarta.annotation.Resource; import net.lab1024.sa.base.module.support.cache.CacheService; import net.lab1024.sa.base.module.support.cache.CaffeineCacheServiceImpl; import net.lab1024.sa.base.module.support.cache.RedisCacheServiceImpl; +import net.lab1024.sa.base.module.support.cache.manager.CustomRedisCacheManager; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.serializer.RedisSerializationContext; @@ -26,15 +29,30 @@ public class CacheConfig { @Resource - private RedisConnectionFactory factory; + private RedisConnectionFactory redisConnectionFactory; + /** + * 创建自定义Redis缓存管理器Bean 整合spring-cache + * Redis连接工厂,用于建立与Redis服务器的连接 + * + * @return CacheManager Redis缓存管理器实例 + */ @Bean @ConditionalOnProperty(prefix = "spring.cache", name = {"type"}, havingValue = REDIS_CACHE) - public RedisCacheConfiguration redisCacheConfiguration() { - return RedisCacheConfiguration.defaultCacheConfig() + public CacheManager cacheManager() { + // 使用非阻塞模式的缓存写入器,适用于大多数高并发场景 + RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); + + // 构建默认缓存配置 + RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig() + // 禁止缓存 null 值,避免缓存穿透 .disableCachingNullValues() - .computePrefixWith(name -> "cache:" + name + ":") - .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer())); + // 使用 FastJSON 序列化缓存值,支持复杂对象 + .serializeValuesWith(RedisSerializationContext.SerializationPair + .fromSerializer(new GenericFastJsonRedisSerializer())); + + // 返回自定义缓存管理器,支持 cacheName#ttl 格式与永久缓存(#-1) + return new CustomRedisCacheManager(redisCacheWriter, defaultCacheConfig); } @Bean diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/RedisCacheServiceImpl.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/RedisCacheServiceImpl.java index 5e04fcca..eda2a328 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/RedisCacheServiceImpl.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/RedisCacheServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; import java.util.Set; @@ -53,7 +54,7 @@ public class RedisCacheServiceImpl implements CacheService { if (keys != null) { return keys.stream().map(key -> { - String redisKey = StrUtil.str(key, "utf-8"); + String redisKey = StrUtil.str(key, StandardCharsets.UTF_8); // 从 Redis 键中提取出最后一个冒号后面的字符串作为真正的键 return redisKey.substring(redisKey.lastIndexOf(":") + 1); }).collect(Collectors.toList()); diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/manager/CustomRedisCacheManager.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/manager/CustomRedisCacheManager.java new file mode 100644 index 00000000..ce959263 --- /dev/null +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/cache/manager/CustomRedisCacheManager.java @@ -0,0 +1,144 @@ +package net.lab1024.sa.base.module.support.cache.manager; + +import cn.hutool.core.util.StrUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.convert.DurationStyle; +import org.springframework.data.redis.cache.*; + +import java.time.Duration; + +import static net.lab1024.sa.base.common.constant.StringConst.COLON; + +/** + * 自定义 RedisCacheManager,支持在 cacheName 中通过 '#' 指定 TTL(过期时间)。 + * @Author CoderKK + * @Date 2025-08-15 13:01:01 + *

+ * 支持格式:{@code cacheName#ttl},其中 ttl 支持 Spring 的 Duration 格式。 + * 特殊值:{@code -1} 表示永久缓存(永不过期)。 + *

+ * + *

使用示例:

+ *
+ * // 10 秒后过期
+ * @Cacheable(value = "user#10s", key = "#id")
+ * // 2 小时后过期
+ * @Cacheable(value = "report#2h", key = "#date")
+ * // 30 分钟后过期
+ * @Cacheable(value = "session#30m", key = "#token")
+ * // 永不过期(永久缓存),适用于极少变化的配置数据
+ * @Cacheable(value = "appConfig#-1", key = "'globalSettings'")
+ * // 无 TTL,使用全局默认过期时间(如 7 天)
+ * @Cacheable(value = "product", key = "#productId")
+ * 
+ * + *

生成的 Redis Key 格式:

+ *
+ * cache:cacheName:key
+ * 例如:cache:user:123
+ *      cache:appConfig:globalSettings
+ * 
+ * + *

支持的 TTL 单位:

+ *
    + *
  • {@code ms} / {@code millis} / {@code milliseconds} - 毫秒
  • + *
  • {@code s} / {@code secs} / {@code seconds} - 秒
  • + *
  • {@code m} / {@code mins} / {@code minutes} - 分钟
  • + *
  • {@code h} / {@code hrs} / {@code hours} - 小时
  • + *
  • {@code d} / {@code days} - 天
  • + *
+ * + *

注意事项:

+ *
    + *
  • 不写单位默认为毫秒
  • + *
  • 永久缓存(#-1)不会自动过期,请配合 @CacheEvict 手动清理。
  • + *
  • 避免对频繁更新的数据使用永久缓存,防止数据陈旧。
  • + *
  • cacheName 中的 '#' 只解析第一个,后续字符将作为 TTL 处理。
  • + *
+ */ +@Slf4j +public class CustomRedisCacheManager extends RedisCacheManager { + + /** + * 缓存全局前缀 + */ + private static final String CACHE_PREFIX = "cache"; + + /** + * 自定义 TTL 分隔符,用于在 cacheName 后附加过期时间 + */ + private static final String CUSTOM_TTL_SEPARATOR = "#"; + + /** + * 默认缓存过期时间:7 天 + */ + private static final Duration DEFAULT_TTL = Duration.ofDays(7); + + public CustomRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) { + super(cacheWriter, defaultCacheConfiguration); + } + + /** + * 创建 RedisCache 实例,支持从 cacheName 解析 TTL + * + * @param name 缓存名称(支持 name#ttl 格式) + * @param cacheConfig 默认缓存配置 + * @return RedisCache + */ + @Override + protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) { + Duration ttl = parseTtlFromCacheName(name); + if (ttl == null) { + ttl = DEFAULT_TTL; + } + + CacheKeyPrefix keyPrefix = cacheName -> { + if (StrUtil.isBlank(cacheName)) { + return CACHE_PREFIX + COLON; + } + String[] parts = cacheName.split(CUSTOM_TTL_SEPARATOR, 2); + String cleanName = StrUtil.trim(parts[0]); + return CACHE_PREFIX + COLON + cleanName + COLON; + }; + + // 构建最终缓存配置:设置 key 前缀 + TTL + RedisCacheConfiguration config = cacheConfig.computePrefixWith(keyPrefix).entryTtl(ttl); + + return super.createRedisCache(name, config); + } + + /** + * 从 cacheName 中解析 TTL + * + * @param name 缓存名称,格式如:users#10m, products#2h, config#-1(永久) + * @return 解析出的 Duration若无效则返回 null;若为 -1,则返回 Duration.ofMillis(-1) 表示永久缓存 + */ + private Duration parseTtlFromCacheName(String name) { + if (StrUtil.isBlank(name)) { + return null; + } + + String[] parts = name.split(CUSTOM_TTL_SEPARATOR, 2); + if (parts.length < 2) { + return null; // 无 TTL 部分 + } + + String ttlStr = StrUtil.trim(parts[1]); + if (StrUtil.isBlank(ttlStr)) { + return null; + } + + // 特殊处理:-1 表示永久缓存 + if ("-1".equals(ttlStr)) { + return Duration.ofMillis(-1); // Spring Redis 中负数 Duration 表示永不过期 + } + + try { + Duration ttl = DurationStyle.detectAndParse(ttlStr); + return ttl.toSeconds() > 0 ? ttl : null; + } catch (IllegalArgumentException e) { + log.debug("解析缓存 TTL 失败,cacheName='{}', ttl='{}', 错误: {}", name, ttlStr, e.getMessage()); + return null; + } + } +} \ No newline at end of file