SpringBoot中如何实现Redis过期事件监听详解?

2026-05-26 06:331阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计641个文字,预计阅读时间需要3分钟。

SpringBoot中如何实现Redis过期事件监听详解?

在Redis配置文件`redis.conf`中,可以通过以下步骤来调整和优化相关配置:

1. 开启Keyspace事件: 在`redis.conf`文件中,找到`notify-keyspace-events`配置项,并将其值设置为包含`K`的选项,例如: notify-keyspace-events K 这将开启Keyspace事件,并允许通过keyspace@前缀来发布事件。

2. 开启Keyevent事件: 同样地,确保`notify-keyspace-events`配置项中包含`E`,例如: notify-keyspace-events KE 这样,就可以通过keyevent@前缀来发布Keyevent事件。

3. 开启通用命令事件: 如果需要监听像DEL、EXPIRE、RENAME等通用命令,确保`notify-keyspace-events`中包含`g`: notify-keyspace-events KEG 这将允许发布与字符串命令相关的通用命令事件。

4. 字符串命令事件: 对于字符串命令,确保`notify-keyspace-events`中包含`$`: notify-keyspace-events KEG$ 这样,就可以监听到所有字符串命令的事件。

完成以上配置后,Redis将开始发布与指定事件类型相关的消息。

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 connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }

3.书写一个监听器

@Slf4j @Component public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); log.info("expiredKey========="+expiredKey); }

4.查询方法中随便加了两个表中的不同id,一个30s,一个27s。

redisUtil.set("UserId"+user.get(0).getId(),user.get(0).getId(),30);
redisUtil.set("UserInfoId"+userInfo.get(0).getId(),userInfo.get(0).getId(),27);

控制台输出:

需要注意的是:

过期监听消息中返回的是,过期的键的key值,是没有返回value的

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

SpringBoot中如何实现Redis过期事件监听详解?

本文共计641个文字,预计阅读时间需要3分钟。

SpringBoot中如何实现Redis过期事件监听详解?

在Redis配置文件`redis.conf`中,可以通过以下步骤来调整和优化相关配置:

1. 开启Keyspace事件: 在`redis.conf`文件中,找到`notify-keyspace-events`配置项,并将其值设置为包含`K`的选项,例如: notify-keyspace-events K 这将开启Keyspace事件,并允许通过keyspace@前缀来发布事件。

2. 开启Keyevent事件: 同样地,确保`notify-keyspace-events`配置项中包含`E`,例如: notify-keyspace-events KE 这样,就可以通过keyevent@前缀来发布Keyevent事件。

3. 开启通用命令事件: 如果需要监听像DEL、EXPIRE、RENAME等通用命令,确保`notify-keyspace-events`中包含`g`: notify-keyspace-events KEG 这将允许发布与字符串命令相关的通用命令事件。

4. 字符串命令事件: 对于字符串命令,确保`notify-keyspace-events`中包含`$`: notify-keyspace-events KEG$ 这样,就可以监听到所有字符串命令的事件。

完成以上配置后,Redis将开始发布与指定事件类型相关的消息。

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 connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }

3.书写一个监听器

@Slf4j @Component public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); log.info("expiredKey========="+expiredKey); }

4.查询方法中随便加了两个表中的不同id,一个30s,一个27s。

redisUtil.set("UserId"+user.get(0).getId(),user.get(0).getId(),30);
redisUtil.set("UserInfoId"+userInfo.get(0).getId(),userInfo.get(0).getId(),27);

控制台输出:

需要注意的是:

过期监听消息中返回的是,过期的键的key值,是没有返回value的

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

SpringBoot中如何实现Redis过期事件监听详解?