当前位置:网站首页>Redis redis 】 【 the expiration of listening
Redis redis 】 【 the expiration of listening
2022-08-09 08:15:00 【Ctrl Trainee - Brother Xie blog】
redisExpired monitoring
使用场景:在我们项目中,Need to put payment for overtime orders,Operations such as updating status and rolling back inventory.在这里,使用redisExpired monitoring can do it
↓↓↓下面上代码↓↓↓
首先需要redisFor the monitoring to take effect, the following code needs to be configured
yml文件

RedisListenerConfig.java
@Slf4j
@Configuration
public class RedisListenerConfig {
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(redisConnectionFactory);
log.info("redisThe monitoring configuration takes effect");
return container;
}
}
使用
RedisKeyExpirationListener.java
@Slf4j
@Configuration
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
@Value("${spring.redis.database}")
private Integer redisDatabase;
private final RedisTemplate<String, String> redisTemplate;
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer, RedisTemplate<String, String> redisTemplate) {
super(listenerContainer);
this.redisTemplate = redisTemplate;
}
@Override
public void onMessage(Message message, byte[] bytes) {
log.info("The expired listener event starts");
RedisSerializer<?> serializer = redisTemplate.getValueSerializer();
String channel = String.valueOf(serializer.deserialize(message.getChannel()));
// 获取过期的Key值
String body = String.valueOf(serializer.deserialize(message.getBody()));
// DB0 key过期监听
if (StrUtil.format("[email protected]{}__:expired", redisDatabase).equals(channel)) {
System.out.println("第" + redisDatabase + "个DB库的key过期,key:" + body);
}
}
}
我们测试一下

等待30秒…
结果如下

总结
如果觉得不错,可以点赞+收藏或者关注下博主.感谢阅读!
边栏推荐
猜你喜欢
随机推荐
3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Exchange、HOOPS Communicator
Object detection app based on appinventor and EasyDL object detection API
我这是来宣传一下
IO字节流读取文本中文乱码
VLAN与静态VLAN的配置
3D精彩案例,清软英泰建成综合轻量化显示平台!
Decimal工具类
scp上传文件到远程服务器
Servlet的实现原理解析(serverapplet)(服务端程序)
[STL]list
Matlab, and nonlinear equations solving linear equations
IP地址及子网划分
204. 数素数
SOLIDWORKS 2022新功能直播揭秘!速来围观!
Non-decreasing Array
浅谈Endpoint
传输层协议介绍
.net(五) 业务层实现
MySql作业练习题
test process









