springboot+redis过期事件监听实现过程解析
1 修改 redis.conf配置文件:
K Keyspace events, published with keyspace@ prefix事件
E Keyevent events, published with keyevent@ prefix
g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, …
$ String commands
l List commands
s Set commands
h Hash commands
z Sorted set commands
x Expired events (events generated every time a key expires)
e Evicted events (events generated when a key is evicted for maxmemory)
A Alias for g$lshzxe, so that the “AKE” string means all the events.
redis.conf 的默认的配置是:notify-keyspace-events ""
我们需要改为:notify-keyspace-events Ex
即对应上面的键的过期事件。修改玩这个重启一下redis
2 客户端来监听redis的过期事件:
@Configuration public class RedisListenerConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }
3.书写一个监听器
@Slf4j @Component public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); log.info("expiredKey========="+expiredKey); }
4.查询方法中随便加了两个表中的不同id,一个30s,一个27s。
redisUtil.set("UserId"+user.get(0).getId(),user.get(0).getId(),30);
redisUtil.set("UserInfoId"+userInfo.get(0).getId(),userInfo.get(0).getId(),27);
控制台输出:
需要注意的是:
过期监听消息中返回的是,过期的键的key值,是没有返回value的
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
- Spring 通过ApplicationListener&ApplicationEvent实现事件监听
- springboot 中事件监听模式的一种实现
- 【SpringBoot】Spring Boot热部署(第7章 Spring Boot项目构建过程解析 第8章 Spring Boot项目热部署的实现 )
- Spring 事务事件监控及实现原理解析
- Spring CGLlB动态代理实现过程解析
- springboot 事件监听的实现方法
- spring cloud config 配置中心快速实现过程解析
- Spring定时任务轮询本地数据库实现过程解析
- java实现简单的窗体和密码验证(传参,事件机制和事件监听的作用过程)
- SpringBoot启动过程源码解析(二)--------------- listener.starting及监听模式的实现
- Spring源码分析----IOC容器的实现(IoC容器的初始化过程(定位、载入解析、注册))
- springboot中自定义异常以及定制异常界面实现过程解析
- Spring Boot + Vue Element实现Excel文件上传、解析、下载(含完整实现过程)
- springboot实现上传并解析Excel过程解析
- Spring 源码解析 ---- 事件监听
- 【spring源码学习】spring的事件发布监听机制源码解析
- Springboot通过aop实现事务控制过程解析
- SpringSecurity rememberme功能实现过程解析
- spring 声明式事务实现过程解析
- spring 实现IOC过程源码解析<一>