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() {
相关推荐
码上成长1 分钟前
长耗时接口异步改造总结
前端·git·后端
李白你好4 分钟前
Redis 漏洞图形化利用工具
数据库·redis·缓存
diudiu96289 分钟前
Logback使用指南
java·开发语言·spring boot·后端·spring·logback
FAFU_kyp24 分钟前
银行技术岗位招聘面试题准备
java·spring boot·spring
Lisonseekpan26 分钟前
Elasticsearch 入门指南
大数据·分布式·后端·elasticsearch·搜索引擎
编程修仙27 分钟前
第十篇 文件上传
spring boot·spring
zhangyifang_00928 分钟前
Spring中的BeanDefinition
java·后端·spring
楠枬37 分钟前
负载均衡 -LoadBalance
后端·spring·spring cloud·负载均衡
milanyangbo38 分钟前
深入解析 Disruptor:从RingBuffer到缓存行填充的底层魔法
java·数据库·后端·架构
赵庆明老师40 分钟前
用缓存功能解决.NET程序访问数据库的性能问题
数据库·缓存·.net