I have use case where single entries need to removed from the cache at a specific time. The TTL needs to be set on a key and not on a cache level
Following this spring redis documentation I tried to implement key specific TTL but it does not work. There is no event happing, I used a listener to check that and there is only an event happing when the cache ttl runs out.
The cached object has a field annotated with @TimeToLive
from
org.springframework.data.redis.core.TimeToLive
looking at the documentation this should trigger an expire event after the time has run out
Cached object
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BrandResponse {
@TimeToLive
private Long ttl;
@NotBlank
private String id;
}
Used dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
Enable Key Space Events
@SpringBootApplication
@ServletComponentScan
@EnableAsync
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class KikaRestApiApplication {
public static void main(String[] args) {
SpringApplication.run(KikaRestApiApplication.class, args);
}
}
The default TTL for the cache is 5 minutes .entryTtl(Duration.ofMinutes(5))
Cache setup
@Configuration
@EnableCaching
public class RedisCachingConfiguration {
private final KikaApiProperties kikaApiProperties;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
public RedisCachingConfiguration(KikaApiProperties kikaApiProperties) {
this.kikaApiProperties = kikaApiProperties;
}
@Bean
public RedisCacheConfiguration cacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(5))
.disableCachingNullValues()
.serializeValuesWith(
SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(host);
configuration.setPort(port);
return new JedisConnectionFactory(configuration);
}
@Bean
public RedisTemplate<String, Idmap> redisTemplate() {
RedisTemplate<String, Idmap> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
}
Is there something I am missing of does @TimeToLive not work together with spring-redis caching.