Reputation: 21114
I'm upgrading a project which uses Spring Data Redis to 2.x. Previously, the cache manager could be configured with a default expiration using RedisCacheManager.setDefaultExpiration(defaultExpireTime). This option does not seem to exist in 2.x. What is the equivalent in 2.x? Is it RedisCacheConfiguration.entryTtl(ttl), or something else?
I'm probably missing something, but I'm not finding a migration guide to Spring Data Redis 2.x. Does such a migration guide exist?
In short, I would like to migrate the following code to Redis 2.x:
public CacheManager cacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
redisCacheManager.setDefaultExpiration(DEFAULT_EXPIRATION_SECONDS);
return redisCacheManager;
}
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
Is the following equivalent? If not, what would be the equivalent code in Redis 2.x?
public CacheManager cacheManager() {
return RedisCacheManager.builder(redisConnectionFactory())
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(DEFAULT_EXPIRATION_DURATION))
.build();
}
Upvotes: 10
Views: 7946
Reputation: 632
With new redis version, you can no longer pass RedisTemplate anymore.
This is how you can set RedisCacheManager expiration (ex: 1 hour expiration)
@Bean
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.entryTtl(Duration.ofHours(1));
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
If no entryTtl is set, there is no expiration.
Tested with spring redis 2.4.1
PS: You can also define two RedisCacheManager bean, one with @Primary annotation. This way you can pass the timeoutCacheManager in the @Cacheable / @CachePut annotation like so.
@Cacheable(value = "your.cache.name", cacheManager = "timeoutCacheManager")
Upvotes: 1
Reputation: 21
put this into application.yml
spring.cache.redis.time-to-live: 60s
change 60 to your DEFAULT_EXPIRATION_SECONDS
Upvotes: 2
Reputation: 179
Originally, I was running the following source code and dependencies...
@Bean(value ="redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(resourceConfigValue.getCacheExpireSeconds());
return cacheManager;
}
org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE
org.springframework.boot:spring-boot-starter-data-redis:1.5.10.RELEASE
I've confirmed this idea is working as expected...
@Bean(value ="redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
Duration expiration = Duration.ofSeconds(resourceConfigValue.getCacheExpireSeconds());
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(expiration)).build();
}
org.springframework.boot:spring-boot-starter-web:2.0.2.RELEASE
org.springframework.boot:spring-boot-starter-data-redis:2.0.2.RELEASE
Upvotes: 9