redis.conf
notify-keyspace-events Ex
KeyExpiredListener.java
public class KeyExpiredListener extendsJedisPubSub {publicKeyExpiredListener() {
}
@Overridepublic void onMessage(String channel, String message) { //收到消息会调用
System.out.println(String.format("receive redis published message, channel %s, message %s", channel, message));
}
@Overridepublic void onSubscribe(String channel, int subscribedChannels) { //订阅了频道会调用
System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d",
channel, subscribedChannels));
}
@Overridepublic void onUnsubscribe(String channel, int subscribedChannels) { //取消订阅 会调用
System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d",
channel, subscribedChannels));
}
}
AppListener.java
public abstract class AppListener extendsJbootAppListenerBase {
@Overridepublic voidonInterceptorConfig(Interceptors interceptors) {super.onInterceptorConfig(interceptors);
interceptors.add(newSessionInViewInterceptor());
}
@Overridepublic voidonEngineConfig(Engine me) {
}
@Overridepublic voidonStart() {
Aop.inject(this);
Jboot.getRedis().subscribe(new KeyExpiredListener(), "__keyevent@0__:expired");
}
}
测试代码
Cache.set("test-101", "101", 30);
Cache.set("test-201", "201", 60);
Cache.set("test-301", "301", 90);
效果
receive redis published message, channel __keyevent@0__:expired, message xxxx:S:test-101
receive redis published message, channel __keyevent@0__:expired, message xxxx:S:test-201
receive redis published message, channel __keyevent@0__:expired, message xxxx:S:test-301