当前位置:   article > 正文

Springboot 注解 @Cacheable自定义单个key设置expire超时时间 并在配置文件里配置_expirea springboot

expirea springboot

Springboot  RedisCacheManager 类的配置 指定key的过期时间 并在配置文件里配置

目的&效果

在springBoot中配置了RedisCache,当使用@Cacheable注解时,默认为redisCache,通过在配置文件里设置不同key的过期时间,达到可自定义key过期时间的效果。

方案

step 1

 新建一个Map类,用于存放要设置的key

  1. @ConfigurationProperties
  2. public class Properties {
  3. private final Map<String, Duration> initCaches = Maps.newHashMap();
  4. public Map<String, Duration> getInitCaches() {
  5. return initCaches;
  6. }
  7. }
step2

在redis配置类里编写cacheManager,并将map set进去

  1. @Autowired
  2. private Properties properties;
  3. @Bean
  4. public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
  5. RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
  6. .entryTtl(Duration.ofMinutes(10)).disableCachingNullValues();
  7. RedisCacheWrit
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/353163?site
推荐阅读
相关标签
  

闽ICP备14008679号