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() {
相关推荐
小北方城市网27 分钟前
MySQL 索引优化实战:从慢查询到高性能
数据库·spring boot·后端·mysql·rabbitmq·mybatis·java-rabbitmq
Chan1627 分钟前
《Java并发编程的艺术》| 并发关键字与 JMM 核心规则
java·开发语言·数据库·spring boot·java-ee·intellij-idea·juc
汤姆yu39 分钟前
基于springboot的植物花卉销售管理系统
java·spring boot·后端
海南java第二人1 小时前
Spring Boot Starters深度解析:简化依赖管理的核心利器
java·spring boot·后端
韩立学长1 小时前
Springboot喵趣网上宠物店的设计和实现5pidz60b(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
让我上个超影吧2 小时前
天机学堂——播放进度方案优化
java·spring boot·redis·spring cloud
深入技术了解原理2 小时前
引入eureka依赖但是无法注册:无法解析配置属性 ‘eureka.client.service-url.defaultZone‘
spring boot·spring cloud·云原生·eureka
biyezuopinvip2 小时前
基于Spring Boot的社区互助平台设计与实现(毕业论文)
java·spring boot·vue·毕业设计·论文·毕业论文·社区互助平台设计与实现
Coder_Boy_2 小时前
基于SpringAI的在线考试系统-试卷管理与考试管理模块联合回归测试文档
人工智能·spring boot·架构·领域驱动
J_liaty2 小时前
Redis公共方法详解
spring boot·redis·后端