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() {
相关推荐
Coder_Boy_15 分钟前
基于SpringAI的智能OPS平台AIops介绍
人工智能·spring boot·aiops·faiss
攻心的子乐26 分钟前
redis 使用Pipelined 管道命令批量操作 减少网络操作次数
数据库·redis·缓存
superman超哥1 小时前
Rust 发布 Crate 到 Crates.io:从本地到生态的完整旅程
开发语言·后端·rust·crate·crates.io
king0071 小时前
Spring-AI 结合自定义 mcp server 实现飞书智能机器人
spring boot·ai·ai编程
Channon_1 小时前
专题四:内存战场的无声战役——压缩、共享与空间复用
缓存·嵌入式·空间复用
长不大的蜡笔小新1 小时前
私人健身房管理系统
java·javascript·spring boot
Clarence Liu1 小时前
用 Go 从 100 亿个数中找到最小的 100 个数 —— 实战与原理
开发语言·后端·golang
MoonBit月兔1 小时前
用 MoonBit 打造的 Luna UI:日本开发者 mizchi 的 Web Components 实践
前端·数据库·mysql·ui·缓存·wasm·moonbit
独自归家的兔2 小时前
基于 cosyvoice-v3-plus 的简单语音合成
人工智能·后端·语音复刻
踏浪无痕2 小时前
从 node-exporter 学如何写出可复用的监控指标
运维·后端·架构