Redis 過期監(jiān)聽場景
業(yè)務(wù)中有類似等待一定時間之后執(zhí)行某種行為的需求 , 比如 30 分鐘之后關(guān)閉訂單 . 網(wǎng)上有很多使用 Redis 過期監(jiān)聽的 Demo
redis配置
把notify-keyspace-events Ex 這一行的注釋打開

項目demo工程
項目結(jié)構(gòu)如下圖

maven依賴
?xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
parent>
artifactId>kim-redis/artifactId>
groupId>com.kim/groupId>
version>1.0.0/version>
/parent>
modelVersion>4.0.0/modelVersion>
artifactId>kim-redis-expiration-notice/artifactId>
dependencies>
dependency>
groupId>org.springframework.boot/groupId>
artifactId>spring-boot-starter-web/artifactId>
/dependency>
dependency>
groupId>org.springframework.boot/groupId>
artifactId>spring-boot-starter-data-redis/artifactId>
/dependency>
dependency>
groupId>org.projectlombok/groupId>
artifactId>lombok/artifactId>
/dependency>
dependency>
groupId>org.apache.commons/groupId>
artifactId>commons-pool2/artifactId>
/dependency>
/dependencies>
/project>
配置文件
server:
port: 20103
spring:
redis:
#數(shù)據(jù)庫索引
database: 0
host: 127.0.0.1
port: 6379
password: 123456
lettuce:
pool:
#最大連接數(shù)
max-active: 8
#最大阻塞等待時間(負數(shù)表示沒限制)
max-wait: -1
#最大空閑
max-idle: 8
#最小空閑
min-idle: 0
#連接超時時間
timeout: 10000
啟動類
/**
* @Project: kim-redis
* @PackageName: com.kim.redis.expiration.notice
* @FileName: NoticeApplication.java
* @Description: The NoticeApplication is...
* @Author: kimwu
* @Time: 2020-12-19 14:01:56
*/
@SpringBootApplication
public class NoticeApplication {
public static void main(String[] args) {
SpringApplication.run(NoticeApplication.class, args);
}
}
配置類
@Configuration
public class RedisTimeoutConfiguration {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer() {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
return redisMessageListenerContainer;
}
@Bean
public KeyExpiredListener keyExpiredListener() {
return new KeyExpiredListener(this.redisMessageListenerContainer());
}
}
監(jiān)聽類
@Slf4j
public class KeyExpiredListener extends KeyExpirationEventMessageListener {
public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
//過期的key
String key = new String(message.getBody(), StandardCharsets.UTF_8);
log.info("redis key 過期:pattern={},channel={},key={}", new String(pattern), channel, key);
}
}
異常情況測試
當key過期時,項目宕機了
①寫入redis的key
②手動關(guān)停服務(wù),等待redis的key過期
③確認redis的key過期后,重啟服務(wù)。服務(wù)不會收到通知
當key過期時,redis服務(wù)宕機了
①寫入redis的key
②關(guān)停redis服務(wù),等待redis的key過期
③啟動redis服務(wù),發(fā)現(xiàn)redis的過期key已經(jīng)不存在了,服務(wù)沒有收到通知
結(jié)論
redis的鍵過期本身不可靠,并不像rabbitmq一樣保證了可靠性。
當服務(wù)本身宕機或者redis宕機時,將無法保證過期的key能夠被消費。
當使用場景對數(shù)據(jù)完整性不那么精確時,可以使用redis的鍵過期策略。否則不太建議使用redis的鍵過期策略。
到此這篇關(guān)于使用redis實現(xiàn)延遲通知功能(Redis過期鍵通知)的文章就介紹到這了,更多相關(guān)Redis過期鍵通知內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- redis學(xué)習之RDB、AOF與復(fù)制時對過期鍵的處理教程
- 大家都應(yīng)該知道的Redis過期鍵與過期策略
- Redis 2.8-4.0過期鍵優(yōu)化過程全紀錄
- redis鍵空間通知使用實現(xiàn)
- Redis開啟鍵空間通知實現(xiàn)超時通知的步驟詳解