当前位置:网站首页>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秒…
结果如下
总结
如果觉得不错,可以点赞+收藏或者关注下博主.感谢阅读!
边栏推荐
猜你喜欢
随机推荐
权限(上)
【MySQL】mysql:解决[Err] 1093 - You can‘t specify target table ‘表名‘ for update in FROM clause问题
【redis】redis之过期监听
Boot process and service control
LeetCode·每日一题·636.函数的独占时间·栈模拟
Literature retrieval operation code
libtorch示例
The MySQL database
IP地址及子网划分
web基本概念
Set集合
Routing configuration forwarding and experiment
数制转换及子网划分
包子凑数----欧几里得+dp
SOLIDWORKS Simulation教程:计算物体的固有频率
3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Exchange、HOOPS Communicator
交换机基本原理与配置
基于appinventor与EasyDL物体检测API的物体检测app
CUDA和cuDNN 安装10.0版本
.net(二) 配置数据库