香港云主机最佳企业级服务商!

ADSL拨号VPS包含了中国大陆(联通,移动,电信,)

中国香港,国外拨号VPS。

当前位置:云主机 > Redis >

电信ADSL拨号VPS
联通ADSL拨号VPS
移动ADSL拨号VPS

Redis开启键空间通知实现超时通知的步骤详解


时间:2020-11-14 13:51 作者:admin


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.179spring.redis.port=6379spring.redis.database=0spring.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配置类

@Configurationpublic 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接收,在该类中即可调用对应的业务方法进行业务处理。

总结

到此这篇关于Redis开启键空间通知实现超时通知的步骤的文章就介绍到这了,更多相关redis键空间通知内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

(责任编辑:admin)






帮助中心
会员注册
找回密码
新闻中心
快捷通道
域名登录面板
虚机登录面板
云主机登录面板
关于我们
关于我们
联系我们
联系方式

售前咨询:17830004266(重庆移动)

企业QQ:383546523

《中华人民共和国工业和信息化部》 编号:ICP备00012341号

Copyright © 2002 -2018 香港云主机 版权所有
声明:香港云主机品牌标志、品牌吉祥物均已注册商标,版权所有,窃用必究

云官方微信

在线客服

  • 企业QQ: 点击这里给我发消息
  • 技术支持:383546523

  • 公司总台电话:17830004266(重庆移动)
  • 售前咨询热线:17830004266(重庆移动)