当前位置:网站首页>Redis配置缓存过期监听事件触发
Redis配置缓存过期监听事件触发
2022-07-29 02:50:00 【林春Ax】
1、配置redis.conf文件中的notify-keyspace-events Ex
2、配置完之后重启redis服务,运行以下代码,开新的窗口设置时效性的键值对,等待它过期发现监听窗口出现信息提式,注意返回的是这个键值对的key不是value,这个后续使用设备号来做key的话过期就可以提取出这个设备Key去关掉它的电源灯来做给用户的一个提示。

3、springboot里需要做的配置类
/**redis缓存配置类*/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}过期触发的业务逻辑(注意里面关闭电源的方式与前面的文章都是连在一块的业务逻辑)
@Component
public class RedisListener extends KeyExpirationEventMessageListener {
@Autowired
private UserDeviceService userDeviceService;
@Autowired
private DeviceService deviceService;
@Autowired
private PubMessage pubMessage;
public RedisListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
/**
* 针对redis数据失效事件,进行数据处理
*
* @param message
* @param pattern
*/
@Override
public void onMessage(Message message, byte[] pattern) {
/**当名为expiredKey的键值对过期了,会把key返回,而不会将value返回*/
String expiredKey = message.toString();
try {
/**首先修改订单使用时间为0*/
boolean updateExOrder = userDeviceService.updateExOrder(expiredKey);
/**其次将设备的锁装置与设备状态释放掉*/
deviceService.freeDeviceByDeviceId(expiredKey);
/**最后向设备查询设备电源状态,如果状态为开则发送关闭指令*/
boolean power = deviceService.findDevicePowerByDeviceId(expiredKey);
if (power) {
/**订单过期,现在关闭端口电源通断*/
pubMessage.sendDownMessage(expiredKey);
/**数据库里记录的电源还开着,所以我们需要将数据库表中的电源关掉**/
boolean downDevicePowerByDeviceId = deviceService.downDevicePowerByDeviceId(expiredKey);
if (!downDevicePowerByDeviceId)
throw new Exception("数据库power更改异常");
}
if (!updateExOrder) {
throw new Exception("订单过期处理失败");
}
} catch (Exception e) {
System.out.println("---------处理过期订单业务失效---------");
}
System.out.println(expiredKey + "订单过期--------------------");
}
}
边栏推荐
- Verilog's time system tasks - $time, $stime, $realtime
- Cloud development pocket toolbox wechat applet source code
- Stm32c8t6 encoder motor speed measurement and Arduino photoelectric module speed measurement
- Etcd implementation of large-scale service governance application practice
- HTB-Blue
- 12_ue4进阶_换一个更好看的人物模型
- 解析Steam教育中的项目式学习创造力
- C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
- 【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
- Day 10 notes
猜你喜欢

Confusion matrix learning notes

第2章 VRP命令行

会议OA之反馈功能

Implementation principle of golang synergy

Notes on the seventh day

Li Shuo, vice president of Baidu: it's a good thing that China's labor costs rise with the support of digital technology

HTB-Blocky

SQL查询数据之多表(关联)查询

Flink kernel source code (VII) Flink SQL submission process

Cloud development workers must go to work fishing and paddling wechat applet source code
随机推荐
MySQL compound query (important)
The basic concept of the origin and connotation of maker Education
C language: hollow square pattern
centos安装mysql8
看机器人教育引领素质教育主流
Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers
向DataFrame中的特定位置添加一行
Mongodb index (3)
Cloud development workers must go to work fishing and paddling wechat applet source code
C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
11. Writing rules - pseudo target
Linux下安装MySQL8.0的详细步骤
[NPM error] - NPM err code eresolve and NPM err eresolve could not resolve problems
R语言ERROR: compilation failed for package ‘****‘
常用hooks总结
Navicat new database
Analysis of concepts and terms in data warehouse
04 | 后台登录:基于账号密码的登录方式(上)
Available data sets for scene classification tasks (part)
A good-looking IAPP donation list source code