Jboot实现Redis操作事件通知
Posted coffee_cn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jboot实现Redis操作事件通知相关的知识,希望对你有一定的参考价值。
redis.conf
notify-keyspace-events Ex
KeyExpiredListener.java
public class KeyExpiredListener extends JedisPubSub { public KeyExpiredListener() { } @Override public void onMessage(String channel, String message) { //收到消息会调用 System.out.println(String.format("receive redis published message, channel %s, message %s", channel, message)); } @Override public void onSubscribe(String channel, int subscribedChannels) { //订阅了频道会调用 System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d", channel, subscribedChannels)); } @Override public 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 extends JbootAppListenerBase { @Override public void onInterceptorConfig(Interceptors interceptors) { super.onInterceptorConfig(interceptors); interceptors.add(new SessionInViewInterceptor()); } @Override public void onEngineConfig(Engine me) { } @Override public void onStart() { 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
以上是关于Jboot实现Redis操作事件通知的主要内容,如果未能解决你的问题,请参考以下文章