10

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();
}
M. Justin
  • 14,487
  • 7
  • 91
  • 130

3 Answers3

9

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

Brian Lee
  • 180
  • 2
  • 8
  • How have you confirmed that? – M. Justin Jun 06 '18 at 20:53
  • 3
    After making these changes I ran my unit test suite, which is about 275 unit and integration testcases. This including accessing values from the Redis Server where I store the Application Context for my security and user object. Everything seems to work as before, now if there is new feature(s), I have not the time to dive deeper yet - but thank you for your example because with all the recent changes in major versions, I'm not seeing the documentation updates and this article helps me get moving forward. – Brian Lee Jun 08 '18 at 17:25
2

put this into application.yml

spring.cache.redis.time-to-live: 60s

change 60 to your DEFAULT_EXPIRATION_SECONDS

1

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")
Francis Robitaille
  • 575
  • 1
  • 5
  • 18