Redis过期通知

一、键空间通知Keyspace Notifications

 1. redis 2.8.0版本之后才支持

2. 基于pub-sub

3. 消息类型:keysapce/keyevent

二、场景

1. redis缓存一般要设置过期时间,否则会越来越多,但是如果缓存还有用,可以使用过期提醒来重新加载缓存

2. 过期时需要做一些逻辑处理

三、redis配置文件

修改redis.conf,设置notify-keyspace-events Ex,默认为notify-keyspace-events "" ,修改后重启redis

四、监听命令

psubscribe __keyevent@0__:expired  //监听第0个数据库

psubscribe __keyevent@*__:expired  //监听所有数据库

五、关键代码

1. 写一个类继承JedisPubSub

class RedisMsgPubSubListener extends JedisPubSub

2. 客户端监听

RedisMsgPubSubListener listener = new RedisMsgPubSubListener();  
jedis.subscribe(listener, "__keyevent@0__:expired"); //subscribe是阻塞方法

参考:

https://blog.csdn.net/weixin_41497481/article/details/85322794

https://blog.csdn.net/zhu_tianwei/article/details/80169900

https://www.cnblogs.com/ruiati/p/6655949.html

猜你喜欢

转载自www.cnblogs.com/june0816/p/11462912.html