I am trying to configure my redis sentinel client to perform my specific jobs on expire event for different keys that I put to the cache.
This jobs should check whether to renew the cache value or to let it expire. Unfortunately, I couldn't find anything like that for redis.
Does redis even support it or there is no way I can do anything on expiration event for values I store in the cache?
My config:
@Bean(destroyMethod = "shutdown")
public RedissonClient redisson() {
Config config = new Config();
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
for (String node : redisNodes) {
sentinelServersConfig.addSentinelAddress(REDIS_PROTOCOL + node);
}
sentinelServersConfig.setMasterName(redisMaster);
return Redisson.create(config);
}
@Bean
public MyCache myCache() {
return new MyCache(redisTemplate(), cacheTTL);
}
private <K, V> RedisTemplate<K, V> redisTemplate() {
RedisTemplate<K, V> template = new RedisTemplate<>();
template.setConnectionFactory(redissonConnectionFactory(redisson()));
GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
StringRedisSerializer keySerializer = new StringRedisSerializer();
template.setKeySerializer(keySerializer);
template.setValueSerializer(valueSerializer);
template.setHashKeySerializer(keySerializer);
template.setHashValueSerializer(valueSerializer);
template.afterPropertiesSet();
return template;
}
The way I use to put elements to the cache:
public void putElement(String key, T value, Duration duration) {
String formattedKey = getKey(key);
redisTemplate.opsForValue().set(formattedKey, value);
redisTemplate.expire(formattedKey, duration.toMillis(), MILLISECONDS);
}
pom.xml
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-18</artifactId>
<version>${redisson-spring-data-version}</version>
</dependency>