redis可以设置过期key回调实现延时队列
2024-09-07 14:15:24
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component; @Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
} /**
* 针对redis数据失效事件,进行数据处理
* @param message
* @param pattern
*/
@Override
public void onMessage(Message message, byte[] pattern) {
// 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
String expiredKey = message.toString();
System.out.println(expiredKey);
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer; @Configuration
public class RedisListenerConfig { @Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
启动工程.
redis.windows-service.conf 文件需要修改
然后在此基础上把notify-keyspace-events Ex 这一行的注释打开
set a 1 ex 2(2秒后过期)
页面就会打印过期的key为a
最新文章
- Android+jsp +html 文件上传案例 已测试 成功通过
- XMPP iOS客户端实现三:登录、注册
- CSS3的高级特性
- d3 API scale
- Java多线程9:ThreadLocal源码剖析
- 一款基于HTML5的Web 3D开发工具
- 【转载】Velocity模板引擎的介绍和基本的模板语言语法使用
- Javascript自动换图片
- Delphi2010新发现-类的构造和析构函数功能
- DBCC CHECKDB用法 手工修复数据库
- 【Spring】BeanFactory解析bean详解
- css3学习系列之选择器(一)
- java集合系列——Map之HashMap介绍(八)
- iOS 播放音频的几种方法
- global与nonlocal关键字
- 20180706001 - 动态添加 tabPage
- 视觉显著性简介 Saliency Detection
- linux文件管理 文件操作
- screen 命令安装使用
- C#精华面试题及答案 三
热门文章
- [AGC023D] Go Home 题解
- FastAPI 学习之路(六)查询参数,字符串的校验
- 【UE4 设计模式】组件模式 Components Pattern
- 力扣 - 剑指 Offer 53 - II. 0~n-1中缺失的数字
- 第五课第四周实验一:Embedding_plus_Positional_encoding 嵌入向量加入位置编码
- NOIP模拟84(多校17)
- 浅谈如何爆踩TLEcoders
- RocketMQ源码详解 | Broker篇 &#183; 其二:文件系统
- 使用NLog把日志写入数据库并按天自动分表
- 不破不立,祝贺EDG夺得S11冠军。这一夜,我看到太多Flag成真