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() {
相关推荐
一只鹿鹿鹿9 分钟前
研发中心数据安全管理规定(文件)
java·运维·开发语言·数据库·后端
忧郁缭绕13 分钟前
Spring Boot Pf4j模块化能力设计思考
java·spring boot·后端
波波00720 分钟前
每日一题:请解释 .NET 中的协变和逆变?
后端·.net
ByNotD0g27 分钟前
深入解析 Go 官方更新:实验性 goroutineleak Profile 原理与机制
开发语言·后端·golang
IT_陈寒30 分钟前
SpringBoot自动配置揭秘:90%开发者不知道的核心原理
前端·人工智能·后端
星轨zb30 分钟前
非遗AI对话系统架构升级实战
java·人工智能·redis·后端·系统架构
iPadiPhone31 分钟前
Spring Boot 核心注解全维度解析与面试复盘
java·spring boot·后端·spring·面试
夫唯不争,故无尤也32 分钟前
curl与Invoke-RestMethod核心区别
后端·fastapi·powershell·curl
彭于晏Yan36 分钟前
Spring Cloud Stream使用
spring boot·后端·spring cloud
DisonTangor39 分钟前
黑森林研究所提出KV缓存方式让生图模型能更好地多参考编辑
人工智能·缓存·ai作画·开源·aigc