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() {
相关推荐
王中阳Go19 小时前
告别调包侠!2026年Go/Java程序员的AI架构师实战转型指南
后端·go
⑩-19 小时前
SpringCloud-Feign&RestTemplate
后端·spring·spring cloud
我是谁的程序员19 小时前
抓包工具有哪些?代理抓包、数据流抓包、拦截转发工具
后端
开心猴爷20 小时前
APP 上架苹果 App Store 被拒,并不总是产品问题
后端
Li_76953220 小时前
Redis —— (五)
java·redis·后端·spring
用户479492835691520 小时前
你每天都在用的 JSON.stringify ,V8 给它开了“加速通道”
前端·chrome·后端
JIngJaneIL20 小时前
基于java+ vue办公管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
uzong20 小时前
如何将项目做出 owner 的感觉
后端
毕设源码-郭学长20 小时前
【开题答辩全过程】以 基于SpringBoot的企业销售合同管理设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
Java编程爱好者20 小时前
同事查日志太慢,我现场教他一套 awk、tail、grep、sed 组合拳
后端