Redis過期監(jiān)聽機(jī)制,訂單超時自動取消方式
Redis過期監(jiān)聽機(jī)制 (Windows)
一、功能介紹
1. redis過期監(jiān)聽:當(dāng)數(shù)據(jù)設(shè)置了過期時間,redis會根據(jù)某些機(jī)制去時時監(jiān)聽過期的數(shù)據(jù)
2. 應(yīng)用場景: 商城中未支付過期的訂單、通知(當(dāng)然也可以用redis的延遲)等等
3. 本篇文章只限于Windows,Linux配置差不多,只是操作系統(tǒng)不同
二、redis過期監(jiān)聽的配置
1. 在redis安裝的目錄下找到redis.windows.conf文件
2. 編輯redis.windows.conf找到配置文件中notify-keyspace-events " " 的值,
修改為notify-keyspace-events Ex(如圖下)

3. 關(guān)閉redis啟動窗口,在redis安裝的目錄下找到start.bat重啟redis (如圖下)

4. 在SpringBoot集成使用
- 1、引入redis相關(guān)依賴(如圖下)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

- 2、創(chuàng)建配置類RedisListenerConfig
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @date 2023-02-21
* @author LIZAN
*/
@Configuration
public class RedisListenerConfig {
@Autowired
private RedisTemplate redisTemplate;
/**
* 處理亂碼
* @return
*/
@Bean
public RedisTemplate redisTemplateInit() {
// key序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
//val實例化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
- 3、繼承KeyExpirationEventMessageListener創(chuàng)建redis過期事件的監(jiān)聽類,實現(xiàn)onMessage方法接收過去redis數(shù)據(jù)
import lombok.extern.slf4j.Slf4j;
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;
/**
* @author LiZan
* @version 1.0
* @date 2023/2/21 14:09
*/
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
/**
* 針對redis數(shù)據(jù)失效事件,進(jìn)行數(shù)據(jù)處理
* @param message 失效的key
*/
@Override
public void onMessage(Message message, byte[] pattern) {
log.info("過期redis數(shù)據(jù):" + message.toString());
try {
String key = message.toString();
//從失效key中篩選代表訂單失效的key
String orderWithKey = "order_";
if (null != key && orderWithKey.startsWith(key)) {
log.info("訂單號為【" + 123456 + "】超時未支付-自動修改為已取消狀態(tài)");
}
} catch (Exception e) {
e.printStackTrace();
log.error("【修改支付訂單過期狀態(tài)異?!浚? + e.getMessage());
}
}
}
- 4、測試Redis過期監(jiān)聽,在redis安裝的目錄下找到redis-cli.exe 打開后執(zhí)行redis語法寫入五秒后過期的測試數(shù)據(jù),SET order_no123213 123 EX 5

- 5、數(shù)據(jù)過期后,進(jìn)入Redis過期監(jiān)聽方法,打印過期數(shù)據(jù)從而實現(xiàn)業(yè)務(wù)

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Redis實現(xiàn)訪問次數(shù)限流的方法詳解
這篇文章主要給大家介紹了關(guān)于如何利用Redis實現(xiàn)訪問次數(shù)限流的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02
redis列表類型_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了redis列表類型的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
基于redis實現(xiàn)世界杯排行榜功能項目實戰(zhàn)
前段時間,做了一個世界杯競猜積分排行榜。對世界杯64場球賽勝負(fù)平進(jìn)行猜測,猜對+1分,錯誤+0分,一人一場只能猜一次。下面通過本文給大家分享基于redis實現(xiàn)世界杯排行榜功能項目實戰(zhàn),感興趣的朋友一起看看吧2018-10-10
redis中隊列消息實現(xiàn)應(yīng)用解耦的方法
這篇文章主要給大家介紹了關(guān)于redis中隊列消息實現(xiàn)應(yīng)用解耦的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09

