First, the key space notice Keyspace Notifications
1. redis only supported version after 2.8.0
2. Based on the pub-sub
3. Message Type: keysapce / keyevent
Two, Scene
1. redis cache generally set the expiration time, otherwise they will be more and more, but if there is a cache can be used to reload the cache expiration reminder
Need to do some processing logic 2. expired
Three, redis profile
Modify redis.conf, set notify-keyspace-events Ex, defaults to notify-keyspace-events "", the revised restart redis
Fourth, the monitor command
psubscribe __keyevent @ 0 __: expired // listening 0th database
psubscribe __keyevent @ * __: expired // monitor all database
Fifth, the key code
1. Write a class that inherits JedisPubSub
class RedisMsgPubSubListener extends JedisPubSub
2. The client monitor
RedisMsgPubSubListener listener = new RedisMsgPubSubListener(); jedis.subscribe(listener, "__keyevent@0__:expired"); //subscribe是阻塞方法
reference:
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