Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I migrate a RedisCacheManager with default expiration to Spring Data Redis 2.0?

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();
}
like image 742
M. Justin Avatar asked Apr 04 '18 15:04

M. Justin


People also ask

How do I store items in Redis cache spring boot?

In spring boot you need to set spring. cache. type=redis and this would create your cache using Redis. @Autowired RedisTemplate<String, String> redisTemplate; redisTemplate.

How does spring boot integrate with Redis cache?

1) Start Redis Server. 2) Start your Spring Boot Application. 3) Make a Rest Call using any REST client on operations which are get, update & delete. You can use any REST client such as any User Interface, RestTemplate or Postman etc.

What is spring boot starter data Redis?

Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.

What is the use of @RedisHash?

@RedisHash can take as parameters a timeToLive of type Long, and a String value, which will override the default Redis key prefix (by default, the key prefix is the fully qualified name of the class plus a colon). Within the class, most of the usual Spring Data annotations are supported.


2 Answers

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

like image 67
Brian Lee Avatar answered Oct 25 '22 16:10

Brian Lee


put this into application.yml

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

change 60 to your DEFAULT_EXPIRATION_SECONDS

like image 34
user11040344 Avatar answered Oct 25 '22 15:10

user11040344