redis的键空间通知(订阅和发布)

redis的key值设置了生命周期后,键值失效触发事件

需要一个线程专门监听redis键值的事件

Jedis jedis=redisTest.getJedis();

for (int i = 0; i < 1; i++) {

TestThread thread= new TestThread(jedis);

thread.start();

}


在线程中配置redis的键空间通知事件:

pool.configSet("notify-keyspace-events", "Ex");

注:

默认配置是这样的:notify-keyspace-events ""

根据文档中的说明:
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.

在线程中添加订阅事件:pool.psubscribe(new MySubscribe(), "*");

事件需要继承redis的JedisPubSub类:

该类中有两个重要的方法:onPSubscribe(String pattern, int subscribedChannels):订阅时触发事件

onPMessage(String pattern, String channel, String message):通知事件发生时触发事件

你可能感兴趣的:(redis的键空间通知(订阅和发布))