java redis监听问题_springboot+redis过期事件监听实现过程解析

本文介绍了如何在Java中使用SpringBoot和Redis实现过期事件监听。首先,需要修改redis.conf配置文件,将`notify-keyspace-events`设置为`Ex`以监听键的过期事件。然后,创建RedisMessageListenerContainer Bean并配置监听器。接着,定义一个监听器`RedisKeyExpirationListener`,在接收到过期消息时打印键值。最后,通过示例展示了如何设置带有不同过期时间的键,并指出监听器只返回过期键的key,不包含value。
摘要由CSDN通过智能技术生成

1 修改 redis.conf配置文件:

K Keyspace events, published with keyspace@ prefix事件

E Keyevent events, published with keyevent@ prefix

g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, …

$ String commands

l List commands

s Set commands

h Hash commands

z Sorted set commands

x Expired events (events generated every time a key expires)

e Evicted events (events generated when a key is evicted for maxmemory)

A Alias for g$lshzxe, so that the “AKE” string means all the events.

redis.conf 的默认的配置是:notify-keyspace-events ""

我们需要改为:notify-keyspace-events Ex

即对应上面的键的过期事件。修改玩这个重启一下redis

2 客户端来监听redis的过期事件:

@Configuration

public class RedisListenerConfig {

@Bean

RedisMessageListenerContainer container(RedisConnectionFactory connecti

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值