码迷,mamicode.com
首页 > 其他好文 > 详细

redis过期key删除

时间:2019-03-07 15:55:03      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:initial   exti   red   初始化   str   psu   删除   jedis   scribe   

LZ一开始配置到启动类里面,结果出现了主线程阻塞的情况。

如下是流程:

首先修改配置文件redis.conf中的:notify-keyspace-events Ex,默认为notify-keyspace-events "" (注释掉默认的,注意notify-keyspace-events Ex之前不能有空格)

启动初始化类里面的contextInitialized方法中加入以下代码:(启动一个线程执行订阅功能)

Thread thread = new Thread(传入需要参数--既然是redis监控必然需要jedis参数);
thread.start();

之后再编写业务代码

resource.psubscribe(pubSub, "__keyevent@0__:expired");

这里是订阅监听,__keyevent@0__:expired是监听的频道

redis监听类

public class Listener extends JedisPubSub{
private static Logger logger = Logger.getLogger(Listener.class);

private XXX xxx;
private YYY yyy;

public Listener(XXX xxx, YYY yyy) {
this.xxx= xxx;
this.yyy= yyy;
}

@Override
public void onPMessage(String pattern, String channel, String message) {
logger.info("onPMessage: channel[" + channel + "], message[" + message + "]");
{业务内容}
}

@Override
public void onMessage(String channel, String message) {
logger.info("onMessage: channel[" + channel + "], message[" + message + "]");
}

@Override
public void onSubscribe(String channel, int subscribedChannels) {
logger.info("onSubscribe: channel[" + channel + "]," + "subscribedChannels[" + subscribedChannels + "]");
}

@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
logger.info("onUnsubscribe: channel[" + channel + "], " + "subscribedChannels[" + subscribedChannels + "]");
}

@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
logger.info("onPUnsubscribe: pattern[" + pattern + "]," + "subscribedChannels[" + subscribedChannels + "]");
}

@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
logger.info("onPSubscribe: pattern[" + pattern + "], " + "subscribedChannels[" + subscribedChannels + "]");
}
}

 

redis过期key删除

标签:initial   exti   red   初始化   str   psu   删除   jedis   scribe   

原文地址:https://www.cnblogs.com/autumn007/p/10489776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!