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();
}
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.
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.
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.
@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.
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
put this into application.yml
spring.cache.redis.time-to-live: 60s
change 60 to your DEFAULT_EXPIRATION_SECONDS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With