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

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

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

- 2、創(chuàng)建配置類(lèi)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實(shí)例化
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過(guò)期事件的監(jiān)聽(tīng)類(lèi),實(shí)現(xiàn)onMessage方法接收過(guò)去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);
}
/**
* 針對(duì)redis數(shù)據(jù)失效事件,進(jìn)行數(shù)據(jù)處理
* @param message 失效的key
*/
@Override
public void onMessage(Message message, byte[] pattern) {
log.info("過(guò)期redis數(shù)據(jù):" + message.toString());
try {
String key = message.toString();
//從失效key中篩選代表訂單失效的key
String orderWithKey = "order_";
if (null != key && orderWithKey.startsWith(key)) {
log.info("訂單號(hào)為【" + 123456 + "】超時(shí)未支付-自動(dòng)修改為已取消狀態(tài)");
}
} catch (Exception e) {
e.printStackTrace();
log.error("【修改支付訂單過(guò)期狀態(tài)異常】:" + e.getMessage());
}
}
}
- 4、測(cè)試Redis過(guò)期監(jiān)聽(tīng),在redis安裝的目錄下找到redis-cli.exe 打開(kāi)后執(zhí)行redis語(yǔ)法寫(xiě)入五秒后過(guò)期的測(cè)試數(shù)據(jù),SET order_no123213 123 EX 5

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

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Windows安裝Redis的幾種方式與測(cè)試流程總結(jié)
本文系統(tǒng)梳理了在 Windows 系統(tǒng)上安裝和使用 Redis 的多種方式,涵蓋通過(guò)端口號(hào)識(shí)別運(yùn)行中的 Redis 實(shí)例、進(jìn)程定位方法,并提供了 Java 環(huán)境下的連接與測(cè)試示例,同時(shí)還介紹了常見(jiàn)的圖形化管理工具,便于可視化管理與調(diào)試,需要的朋友可以參考下2025-05-05
Redis可視化工具Redis?Desktop?Manager的具體使用
本文主要介紹了Redis可視化工具Redis?Desktop?Manager的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Redis Sentinel實(shí)現(xiàn)哨兵模式搭建小結(jié)
這篇文章主要介紹了Redis Sentinel實(shí)現(xiàn)哨兵模式搭建小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Redis基礎(chǔ)學(xué)習(xí)之管道機(jī)制詳析
這篇文章主要給大家介紹了關(guān)于Redis基礎(chǔ)學(xué)習(xí)之管道機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Linux系統(tǒng)下安裝Redis數(shù)據(jù)庫(kù)過(guò)程
大家好,本篇文章主要講的是Linux系統(tǒng)下安裝Redis數(shù)據(jù)庫(kù)過(guò)程,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2021-12-12

