`I want to activate Redis expiry events with Redis cluster having 3 nodes using spring-boot apllication
Added all configurations CONFIG SET notify-keyspace-events Ex
to verify CONFIG GET notify-keyspace-events
Also when trying with PUBLISH keyevent@0:expired "manualTestKey" I am getting event, but when key expires getting no events.
Also added this feature to Redis.conf file as well
Classes used:
@Configuration
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
@Slf4j
public class CacheConfig {
@Value("${spring.cache.redis.time-to-live:PT5M}")
private Duration ttl;
@Value("${spring.cache.redis.key-prefix:checkout::}")
private String prefix;
@Value("${spring.cache.redis.use-key-prefix:true}")
private Boolean useKeyPrefix;
@Value("${spring.redis.cluster.nodes:localhost}")
private String hosts;
@Bean
@Primary
public RedisConnectionFactory redisConnectionFactory() {
var clusterNodes = new ArrayList<String>(Arrays.asList(hosts.split(",")));
var topologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enableAllAdaptiveRefreshTriggers()
.enablePeriodicRefresh().dynamicRefreshSources(true).build();
var clientConfiguration = LettuceClientConfiguration.builder()
.clientOptions(ClusterClientOptions.builder()
.socketOptions(SocketOptions.builder().connectTimeout(Duration.ofSeconds(5)).build())
.topologyRefreshOptions(topologyRefreshOptions).build())
mandTimeout(Duration.ofSeconds(10)).build();
return new LettuceConnectionFactory(new RedisClusterConfiguration(clusterNodes), clientConfiguration);
}
@Bean
@Primary
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
var config = RedisCacheConfiguration.defaultCacheConfig();
var serializer = new GenericJackson2JsonRedisSerializer();
var builder = RedisSerializationContext.<String, Object>newSerializationContext(new StringRedisSerializer());
var context = builder.value(serializer).build();
config = config.serializeValuesWith(context.getValueSerializationPair());
config = config.entryTtl(ttl);
config = config.prefixCacheNameWith(prefix);
if (!(useKeyPrefix)) {
config = config.disableKeyPrefix();
}
return new RedisCacheManager(new CheckoutRedisCacheWriter(connectionFactory), config);
}
@Bean
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory,
RedisExpirationListenerService listener) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(new MessageListenerAdapter(listener), new PatternTopic("__keyevent@*__:expired"));
log.info("Redis expiration listener subscribed to __keyevent@0__:expired");
return container;
}
}```
Listener class:
`java`
@Service
@Slf4j
public class RedisExpirationListenerService implements MessageListener {
@Override
public void onMessage(Message message, byte[] pattern) {
String expiredKey = message.toString();
log.info("Redis key expired: {}", message);
if (expiredKey.endsWith("_dsm")) {
log.info("Triggering call to expire slot for: {}", expiredKey);
//triggerApiCall(expiredKey);
}
}`