1、使用redis key 过期通知,需开启key过期通知功能:
登录进入redis-cli 客户端,执行:
config set notify-keyspace-events Ex
【或者启动redis前在redis.conf 添加配置:notify-keyspace-events "Ex" //过期时间监听生效】
2、启动监听key过期
登录进入redis-cli 客户端,执行:( 订阅一个或者多个符合pattern格式的频道)
PSUBSCRIBE __keyevent@*__:expired
然后该客户端处于监听状态,如果有key过期,则列出
Redis中发布与订阅只有简单的6个命令。即:
订阅一个或者多个符合pattern格式的频道
PSUBSCRIBE pattern [pattern ...]
发布消息到chanel中
PUBLISH channel message
查看订阅与发布系统状态
PUBSUB subcommand [argument [argument ...]]
退订所有符合格式的频道
PUNSUBSCRIBE [pattern [pattern ...]]
订阅一个或者多个频道
SUBSCRIBE channel [channel ...]
取消订阅频道
UNSUBSCRIBE [channel [channel ...]]
--------------------
测试:
3、另外开启一个客户端,添加test 为key,并设置5秒过期
setex test 5 test
5秒后,监听窗口显示:
pmessage
__keyevent@*__:expired
__keyevent@0__:expired
test
java 代码:
先测试一下发布订阅
1、订阅消息的类
import redis.clients.jedis.JedisPubSub;
//订阅消息的处理类
public class SubScriber extends JedisPubSub {
@Override //收到消息会调用
public void onMessage(String channel, String message) {
System.out.println(String.format("收到消息: channel %s, message %s", channel, message));
}
@Override //订阅了频道会调用
public void onSubscribe(String channel, int subscribedChannels) {
System.out.println(String.format("订阅了频道: channel %s, subscribedChannels %d",
channel, subscribedChannels));
}
@Override //取消订阅 会调用
public void onUnsubscribe(String channel, int subscribedChannels) {
System.out.println(String.format("取消订阅: channel %s, subscribedChannels %d",
channel, subscribedChannels));
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
System.out.println("on PSubscribe " + pattern + " " + subscribedChannels);
}
@Override //收到消息会调用
public void onPMessage(String pattern, String channel, String message) {
System.out.println(
"pattern = [" + pattern + "], channel = [" + channel + "], message = [" + message + "]");
//收到消息 key的键值,处理过期提醒
}
}
2、订阅mychannel
private final Subscriber subscriber = new Subscriber();
jedis.subscribe(subscriber, "mychannel"); //订阅mychannel, 订阅后当前线程处于租塞状态,等等消息
3、在 mychannel 的频道上推送消息
jedis.publish("mychannel","abc"); //在 mychannel 的频道上推送消息
测试
public static void main(String[] args) throws InterruptedException {
Jediss jedis = Redis.getUserJedis();
PubSubScriber subscriber = new PubSubScriber();
Thread t = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
jedis.publish("mychannel", "abc - " + i); //在 mychannel 的频道上推送消息
}
}
});
t.start();
jedis.subscribe(subscriber, "mychannel"); // 订阅mychannel, 订阅后当前线程处于租塞状态,等等消息
}
结果:
订阅了频道: channel mychannel, subscribedChannels 1
收到消息: channel mychannel, message abc - 0
收到消息: channel mychannel, message abc - 1
4、订阅键的过期时间的键值
__keyevent@0__:expired
测试: (注意先 config set notify-keyspace-events Ex)
//PSUBSCRIBE __keyevent@*__:expired
public static void main(String[] args) throws InterruptedException {
Jediss jedis = Redis.getUserJedis();
PubSubScriber subscriber = new PubSubScriber();
Thread t = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
jedis.setex("test:ex:"+i,3,"abc"); //3秒过期
System.out.println("test:ex:"+i);
}
}
});
t.start();
jedis.psubscribe(subscriber, "__keyevent@*__:expired"); // 订阅 __keyevent@*__:expired, 订阅后当前线程处于租塞状态,等等消息
}
on PSubscribe __keyevent@*__:expired 1
test:ex:0
pattern = [__keyevent@*__:expired], channel = [__keyevent@1__:expired], message = [test:ex:0]
test:ex:1
test:ex:2
pattern = [__keyevent@*__:expired], channel = [__keyevent@1__:expired], message = [test:ex:1]
pattern = [__keyevent@*__:expired], channel = [__keyevent@1__:expired], message = [test:ex:2]