redis监控key失效
redis修改redis.windows.conf配置文件
放开notify-keyspace-events Ex
springboot + redis 的情况下的监听:
@Configuration public class RedisListenerConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }
实现监听
@Component @Slf4j public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); try{ System.out.println("redis失效key====="+expiredKey); }catch (Exception e){ log.error("redis 消息订阅异常:{}",e.getMessage()); } } }