金狮镖局 Design By www.egabc.com
Redis部分设置
修改配置文件redis.conf(Windows为redis.windows.conf)
- 打开该配置文件(位置取决于自己的安装位置),找到Event notification部分。
- 将notify-keyspace-events Ex的注释打开或者添加该配置,其中E代表Keyevent,此种通知会返回key的名字,x代表超时事件。
- 如果notify-keyspace-events ""配置没有被注释的话要注释掉,否则不会生效。
- 保存后重启redis,一定要使用当前配置文件重启,例如src/redis-server redis.conf
SpringBoot部分设置
添加redis依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
在全局配置文件application中添加redis配置
spring.redis.host = 39.105.145.179 spring.redis.port=6379 spring.redis.database=0 spring.redis.listen-pattern = __keyevent@0__:expired
listen-pattern填写超时时间,意思为springboot将监听redis发出的超时键空间通知。
创建listener
public class TopicMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] bytes) { byte[] body = message.getBody(); byte[] channel = message.getChannel(); System.out.println(new String(body)); System.out.println(new String(channel)); } }
其中message为redis返回的通知,body为超时的key的名字,channel为超时事件
创建listener配置类
@Configuration public class RedisListenerConfiguration { @Bean public RedisMessageListenerContainer getListenerContainer(RedisConnectionFactory connectionFactory){ //创建连接容器 RedisMessageListenerContainer container = new RedisMessageListenerContainer(); //放入redis连接 container.setConnectionFactory(connectionFactory); //写入需要被监听的类型,即超时监听 Topic topic = new PatternTopic("__keyevent@0__:expired"); container.addMessageListener(new TopicMessageListener(), topic); return container; } }
之后当有键值过期时,redis会发送通知被上面的TopicMessageListener接收,在该类中即可调用对应的业务方法进行业务处理。
总结
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无Redis开启键空间通知实现超时通知的步骤详解的评论...
更新日志
2024年06月03日
2024年06月03日
- 罗斯特洛波维奇、布里顿《舒伯特-阿佩乔尼奏鸣曲》1995[WAV+CUE]
- 群星《超高品质流行女声大碟 静听女人心 2CD》[WAV+CUE][1.3GB]
- BEYOND《无悔这一生 2023》 [WAV+CUE][690MB]
- 群星《30首最发烧经典老歌 情牵男人心 2CD》[WAV+CUE][1.4GB]
- 群星《不老的情歌-至尊版2CD》WAV分轨
- 岁月留声机群星《七十年代人金曲回顾》(黑胶)2CD[WAV分轨]
- 群星-世纪难忘金曲(迪安唱片)5CD[WAV]
- HenrietteFaure-DebussyEstampesPrludes,LivreI(MonoVersion)[FLAC]
- 吕方1992-弯弯十八首[香港][WAV+CUE]
- 经典歌曲珍藏专辑《民歌30年·真情依旧》3CD[WAV+CUE]
- 群星《时尚流行发烧极品 非同凡响 第一辑 2CD》[WAV/分轨][1.3GB]
- 陈曦《多年以后》HQⅡ头版限量编号[WAV+CUE][520MB]
- 孙露《用情 HQⅡ 2023》[WAV+CUE][380MB]
- 电影原声《速度与激情9电影原声大碟》2021[WAV+CUE]
- 【瑞鸣音乐】群星《伶歌》[XRCD2][WAV+CUE]