springboot 使用注解设置缓存时效

springboot 使用注解设置缓存时效

bash 复制代码
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;

import java.time.Duration;

public class CustomRedisCacheManager extends RedisCacheManager {

    /*
     * @description 提供默认构造器
     * @author xianping
     * @date 2020/9/28 9:22
     * @param
     * @param cacheWriter
     * @param defaultCacheConfiguration
     * @return
     **/
    public CustomRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
        super(cacheWriter, defaultCacheConfiguration);
    }

    /*
     * @description 重写父类createRedisCache方法
     * @author xianping
     * @date 2020/9/28 9:22
     * @param
     * @param name @Cacheable中的value
     * @param cacheConfig
     * @return org.springframework.data.redis.cache.RedisCache
     **/
    @Override
    protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
        //名称中存在#标记进行到期时间配置
        if (!name.isEmpty() && name.contains("#")) {
            String[] SPEL = name.split("#");

            if (StringUtils.isNumeric(SPEL[1])) {
                //配置缓存到期时间
                int cycle = Integer.parseInt(SPEL[1]);
                return super.createRedisCache(SPEL[0], cacheConfig.entryTtl(Duration.ofMinutes(cycle * 24 * 60)));
            }
        }
        return super.createRedisCache(name, cacheConfig);
    }
}
bash 复制代码
 @PostMapping("/getVehicleMap")
    @Cacheable(value = "getVehicle#1")
    @Operation(summary = "交通工具类型获取")
    public R<JSONArray> getVehicleMap() {
相关推荐
每天进步一点_JL16 小时前
事务与消息中间件:分布式系统中的可见性边界问题
分布式·后端
bcbnb16 小时前
Ipa Guard 集成到 CICD 流程,让 iOS 加固进入自动化时代的完整工程方案
后端
该用户已不存在16 小时前
2025 年 8 款最佳远程协作工具
前端·后端·远程工作
云渠道商yunshuguoji16 小时前
阿里云渠道商:阿里云服务器出问题如何处理?
后端
dyw0817 小时前
如何通过xshell实现建立反向隧道,通过云服务器的访问本地服务
后端
changflow17 小时前
告别“黑盒”等待:如何在 LangGraph 中优雅地实现前端友好的 Human-in-the-Loop?
后端
惜棠17 小时前
visual code + rust入门指南
开发语言·后端·rust
n***i9517 小时前
Rust在嵌入式系统中的内存管理
开发语言·后端·rust
踏浪无痕18 小时前
PageHelper 防坑指南:从兜底方案到根治方案
spring boot·后端
ziwu18 小时前
昆虫识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
后端·图像识别