spring?boot?redis中的key失效監(jiān)聽的問題解決
1、spring boot連接配置redis參考這篇文章
http://www.dbjr.com.cn/program/339977yti.htm
2、首先開啟redis的事件失效推送功能,如果是redis容器啟動,參考下面的方式啟動容器
docker run --restart=always -itd --name redis -p 6379:6379 redis redis-server --notify-keyspace-events Ex --requirepass xxxxxx
然后在redis客戶端下面使用如下方式驗證
CONFIG GET notify-keyspace-events
3、在spring boot 中新建服務(wù)類RedisKeyExpirationListener,用于捕捉獲取redis中的key失效
package com.example.redis_sub.config; 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; @Slf4j @Component public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { // 用戶做自己的業(yè)務(wù)處理即可,注意message.toString()可以獲取失效的key System.out.println("key失效"); String expiredKey = message.toString(); System.out.println(expiredKey); System.out.println(new String(pattern)); //業(yè)務(wù)邏輯 } }
4、新建配置類RedisConfig
package com.example.redis_sub.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; @Configuration public class RedisConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); // 事件以__keyevent@<db>__為前綴進行發(fā)布 // container.addMessageListener(new RedisKeyExpirationListener(container), new PatternTopic("__keyevent@0__" + // ":expired")); container.addMessageListener(new RedisKeyExpirationListener(container), new PatternTopic("__keyevent@0__" + ":expired")); return container; } }
到此這篇關(guān)于spring boot redis中的key失效監(jiān)聽的問題解決的文章就介紹到這了,更多相關(guān)springboot redis key失效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
application.yml文件中如何開啟mybatis自動駝峰映射
這篇文章主要介紹了application.yml文件中開啟mybatis自動駝峰映射的方法,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08Java?LocalDateTime獲取時間信息、格式化、轉(zhuǎn)換為數(shù)字時間戳代碼示例
其實我們在Java項目中對日期進行格式化,主要是利用一些日期格式化類,下面這篇文章主要給大家介紹了關(guān)于Java?LocalDateTime獲取時間信息、格式化、轉(zhuǎn)換為數(shù)字時間戳的相關(guān)資料,需要的朋友可以參考下2023-11-11Springboot+Shiro+Mybatis+mysql實現(xiàn)權(quán)限安全認證的示例代碼
Shiro是Apache?的一個強大且易用的Java安全框架,執(zhí)行身份驗證、授權(quán)、密碼學和會話管理,Shiro?主要分為兩個部分就是認證和授權(quán)兩部分,這篇文章主要介紹了Springboot+Shiro+Mybatis+mysql實現(xiàn)權(quán)限安全認證的示例代碼,需要的朋友可以參考下2024-07-07解決springboot中配置過濾器以及可能出現(xiàn)的問題
這篇文章主要介紹了解決springboot中配置過濾器以及可能出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09