spring?boot?redis中的key失效監(jiān)聽(tīng)的問(wèn)題解決
1、spring boot連接配置redis參考這篇文章
http://m.fzitv.net/program/339977yti.htm
2、首先開(kāi)啟redis的事件失效推送功能,如果是redis容器啟動(dòng),參考下面的方式啟動(dòng)容器
docker run --restart=always -itd --name redis -p 6379:6379 redis redis-server --notify-keyspace-events Ex --requirepass xxxxxx
然后在redis客戶端下面使用如下方式驗(yàn)證
CONFIG GET notify-keyspace-events
3、在spring boot 中新建服務(wù)類(lèi)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、新建配置類(lèi)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>__為前綴進(jìn)行發(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)聽(tīng)的問(wèn)題解決的文章就介紹到這了,更多相關(guān)springboot redis key失效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合redis進(jìn)行數(shù)據(jù)操作(推薦)
springboot整合redis比較簡(jiǎn)單,并且使用redistemplate可以讓我們更加方便的對(duì)數(shù)據(jù)進(jìn)行操作。下面通過(guò)本文給大家分享springboot整合redis進(jìn)行數(shù)據(jù)操作的相關(guān)知識(shí),感興趣的朋友一起看看吧2017-10-10
關(guān)于后端如何解決跨域的問(wèn)題說(shuō)明
這篇文章主要介紹了關(guān)于后端如何解決跨域的問(wèn)題說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心的方法
本文主要介紹了SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Java實(shí)現(xiàn)base64圖片編碼數(shù)據(jù)轉(zhuǎn)換為本地圖片的方法
這篇文章主要介紹了Java實(shí)現(xiàn)base64圖片編碼數(shù)據(jù)轉(zhuǎn)換為本地圖片的方法,涉及java編碼轉(zhuǎn)換及圖片文件生成相關(guān)操作技巧,需要的朋友可以參考下2018-06-06

