redis过期key删除
Posted 一见钟情算不算爱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis过期key删除相关的知识,希望对你有一定的参考价值。
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, "[email protected]__:expired");
这里是订阅监听,[email protected]__: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删除的主要内容,如果未能解决你的问题,请参考以下文章