mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-17 19:06:39 +08:00
feat(cache): 实现自定义 Spring Cache 缓存管理器支持 TTL- 新增 CustomRedisCacheManager 类,用于解析 cacheName 中的 TTL 设置
- 修改 CacheConfig,使用新的自定义缓存管理器 - 更新 RedisCacheServiceImpl,使用 StandardCharsets.UTF_8 替代 魔法值"utf-8"
This commit is contained in:
parent
3dcad0b78a
commit
2eb3742063
@ -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.CacheService;
|
||||||
import net.lab1024.sa.base.module.support.cache.CaffeineCacheServiceImpl;
|
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.RedisCacheServiceImpl;
|
||||||
|
import net.lab1024.sa.base.module.support.cache.manager.CustomRedisCacheManager;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
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.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||||
|
|
||||||
@ -26,15 +29,30 @@ public class CacheConfig {
|
|||||||
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RedisConnectionFactory factory;
|
private RedisConnectionFactory redisConnectionFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建自定义Redis缓存管理器Bean 整合spring-cache
|
||||||
|
* Redis连接工厂,用于建立与Redis服务器的连接
|
||||||
|
*
|
||||||
|
* @return CacheManager Redis缓存管理器实例
|
||||||
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnProperty(prefix = "spring.cache", name = {"type"}, havingValue = REDIS_CACHE)
|
@ConditionalOnProperty(prefix = "spring.cache", name = {"type"}, havingValue = REDIS_CACHE)
|
||||||
public RedisCacheConfiguration redisCacheConfiguration() {
|
public CacheManager cacheManager() {
|
||||||
return RedisCacheConfiguration.defaultCacheConfig()
|
// 使用非阻塞模式的缓存写入器,适用于大多数高并发场景
|
||||||
|
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
|
||||||
|
|
||||||
|
// 构建默认缓存配置
|
||||||
|
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
|
||||||
|
// 禁止缓存 null 值,避免缓存穿透
|
||||||
.disableCachingNullValues()
|
.disableCachingNullValues()
|
||||||
.computePrefixWith(name -> "cache:" + name + ":")
|
// 使用 FastJSON 序列化缓存值,支持复杂对象
|
||||||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));
|
.serializeValuesWith(RedisSerializationContext.SerializationPair
|
||||||
|
.fromSerializer(new GenericFastJsonRedisSerializer()));
|
||||||
|
|
||||||
|
// 返回自定义缓存管理器,支持 cacheName#ttl 格式与永久缓存(#-1)
|
||||||
|
return new CustomRedisCacheManager(redisCacheWriter, defaultCacheConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -10,6 +10,7 @@ import org.springframework.data.redis.cache.RedisCacheManager;
|
|||||||
import org.springframework.data.redis.connection.RedisConnection;
|
import org.springframework.data.redis.connection.RedisConnection;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -53,7 +54,7 @@ public class RedisCacheServiceImpl implements CacheService {
|
|||||||
|
|
||||||
if (keys != null) {
|
if (keys != null) {
|
||||||
return keys.stream().map(key -> {
|
return keys.stream().map(key -> {
|
||||||
String redisKey = StrUtil.str(key, "utf-8");
|
String redisKey = StrUtil.str(key, StandardCharsets.UTF_8);
|
||||||
// 从 Redis 键中提取出最后一个冒号后面的字符串作为真正的键
|
// 从 Redis 键中提取出最后一个冒号后面的字符串作为真正的键
|
||||||
return redisKey.substring(redisKey.lastIndexOf(":") + 1);
|
return redisKey.substring(redisKey.lastIndexOf(":") + 1);
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
|
@ -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
|
||||||
|
* <p>
|
||||||
|
* 支持格式:{@code cacheName#ttl},其中 ttl 支持 Spring 的 Duration 格式。
|
||||||
|
* 特殊值:{@code -1} 表示永久缓存(永不过期)。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <h3>使用示例:</h3>
|
||||||
|
* <pre>
|
||||||
|
* // 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")
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* <h3>生成的 Redis Key 格式:</h3>
|
||||||
|
* <pre>
|
||||||
|
* cache:cacheName:key
|
||||||
|
* 例如:cache:user:123
|
||||||
|
* cache:appConfig:globalSettings
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* <h3>支持的 TTL 单位:</h3>
|
||||||
|
* <ul>
|
||||||
|
* <li>{@code ms} / {@code millis} / {@code milliseconds} - 毫秒</li>
|
||||||
|
* <li>{@code s} / {@code secs} / {@code seconds} - 秒</li>
|
||||||
|
* <li>{@code m} / {@code mins} / {@code minutes} - 分钟</li>
|
||||||
|
* <li>{@code h} / {@code hrs} / {@code hours} - 小时</li>
|
||||||
|
* <li>{@code d} / {@code days} - 天</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <h3>注意事项:</h3>
|
||||||
|
* <ul>
|
||||||
|
* <li>不写单位默认为毫秒</li>
|
||||||
|
* <li>永久缓存(#-1)不会自动过期,请配合 @CacheEvict 手动清理。</li>
|
||||||
|
* <li>避免对频繁更新的数据使用永久缓存,防止数据陈旧。</li>
|
||||||
|
* <li>cacheName 中的 '#' 只解析第一个,后续字符将作为 TTL 处理。</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user