当前位置:网站首页>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 + "订单过期--------------------");
}
}
边栏推荐
- Analysis of OWT server source code (III) -- video module analysis of mixer in
- C语言小项目 -- 通讯录(静态版+动态版+文件版)
- 并发模式之异步回调Future模式
- Restfulapi - C - add header username and password authentication
- 创客教育的起源和内涵的基本理念
- HTB-Blue
- The basic concept of the origin and connotation of maker Education
- 百度副总裁李硕:数字技术加持下中国劳动力成本上升是好事
- Cloud development pocket toolbox wechat applet source code
- Interpretation of ue4.25 slate source code
猜你喜欢

Advanced architects, 16 common principles of microservice design and Governance

cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.

【机器人学习】机械臂抓手matlab运动学与admas动力学分析

HTB-Blue

Weekly recommended short videos: how to make product development more effective?

算法---粉刷房子(Kotlin)

C language: hollow square pattern

Cloud development workers must go to work fishing and paddling wechat applet source code

关于时间复杂度的一些新认识

Thirty years of MPEG audio coding
随机推荐
MySQL compound query (important)
VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
C#从网址异步获得json格式的数据
Li Shuo, vice president of Baidu: it's a good thing that China's labor costs rise with the support of digital technology
Navicat new database
Idea replaces the contents of all files
C语言:空心正方形图案
IDEA安装后无法启动
Algorithm --- paint the house (kotlin)
Summary of classic problems in Flink production environment
Chapter 09_ Use of performance analysis tools
JVM基础入门篇一(内存结构)
Some new ideas about time complexity
Pgzero飞机大战
Look at robot education and lead the mainstream of quality education
Seed random seed
R语言ERROR: compilation failed for package ‘****‘
vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6
My approval function of conference OA project
并发模式之生产者消费者模式