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() {
相关推荐
IT_陈寒1 分钟前
Vue3性能优化实战:这7个技巧让我的应用加载速度提升50%!
前端·人工智能·后端
小宁爱Python2 分钟前
Django Web 开发系列(一):视图基础与 URL 路由配置全解析
后端·python·django
Json____15 分钟前
学习springBoot框架-开发一个酒店管理系统,来熟悉springboot框架语法~
spring boot·后端·学习
Victor35620 分钟前
Redis(74)Redis分布式锁与ZooKeeper分布式锁有何区别?
后端
风象南21 分钟前
SpringBoot “分身术”:同时监听多个端口
后端
Victor35624 分钟前
Redis(75)Redis分布式锁的性能如何优化?
后端
JaguarJack26 分钟前
PHP 8.5 新特性 闭包可以作为常量表达式了
后端·php
毕业设计制作和分享3 小时前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
编啊编程啊程5 小时前
【011】宠物共享平台
spring boot·log4j·maven·dubbo·宠物
你的人类朋友8 小时前
【Node】认识multer库
前端·javascript·后端