赞
踩
Springboot RedisCacheManager 类的配置 指定key的过期时间 并在配置文件里配置
目的&效果
在springBoot中配置了RedisCache,当使用@Cacheable注解时,默认为redisCache,通过在配置文件里设置不同key的过期时间,达到可自定义key过期时间的效果。
方案
step 1
新建一个Map类,用于存放要设置的key
- @ConfigurationProperties
- public class Properties {
-
- private final Map<String, Duration> initCaches = Maps.newHashMap();
-
- public Map<String, Duration> getInitCaches() {
- return initCaches;
- }
- }
step2
在redis配置类里编写cacheManager,并将map set进去
- @Autowired
- private Properties properties;
-
- @Bean
- public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
- RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
- .entryTtl(Duration.ofMinutes(10)).disableCachingNullValues();
-
- RedisCacheWrit
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。