RedisKey的失效監(jiān)聽器KeyExpirationEventMessageListener問題
RedisKey的失效監(jiān)聽器KeyExpirationEventMessageListener
利用KeyExpirationEventMessageListener實現(xiàn)redis的key失效監(jiān)聽。
在使用redis時,所有的key都要設(shè)置過期時間,過期之后,redis就會把對應(yīng)的key清除掉。
此方法可以監(jiān)聽redis的key失效,在失效時做一些邏輯處理。話不多說,上代碼。
依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
實現(xiàn)
import com.upbim.twin.park.common.constans.Constants; import com.upbim.twin.park.server.strategy.PushMessageStrategyContext; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.Topic; import org.springframework.stereotype.Service; import java.util.UUID; import static com.upbim.twin.park.common.constans.Constants.TRACE_ID; @Slf4j @Service public class AgentOrderRedisKeyExpiredListener extends KeyExpirationEventMessageListener { @Autowired private PushMessageStrategyContext pushMessageStrategyContext; @Autowired private RedisProperties redisProperties; /** * Creates new MessageListener for {@code __keyevent@*__:expired} messages. * * @param listenerContainer must not be {@literal null}. */ public AgentOrderRedisKeyExpiredListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override protected void doRegister(RedisMessageListenerContainer listenerContainer) { // 只監(jiān)聽指定redis數(shù)據(jù)庫的key過期事件 Topic topic = new PatternTopic(String.format("__keyevent@%s__:expired", redisProperties.getDatabase())); listenerContainer.addMessageListener(this, topic); } @Override protected void doHandleMessage(Message message) { // 訂閱的topic: new String(message.getChannel(), UTF_8) ---> "__keyevent@0__:expired" // 觸發(fā)key失效發(fā)布的key:new String(message.getBody(), UTF_8) ---> "demoKey" MDC.put(TRACE_ID, UUID.randomUUID().toString()); try { log.info(String.format("Redis key expired event:%s", message.toString())); String beanName = message.toString().split(Constants.COLON)[Constants.ONE]; pushMessageStrategyContext.handleRedisExpireKey(beanName, message.toString()); super.doHandleMessage(message); } finally { MDC.clear(); } } }
監(jiān)聽處理類
package com.upbim.twin.park.server.strategy; import com.google.common.collect.Maps; import com.upbim.twin.park.server.strategy.nozzle.PushMessageStrategy; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; import java.util.Objects; @Slf4j @Component public class PushMessageStrategyContext { /** * 使用線程安全的ConcurrentHashMap存儲所有實現(xiàn)Strategy接口的Bean * key:beanName * value:實現(xiàn)Strategy接口Bean */ private final Map<String, PushMessageStrategy> strategyMap = Maps.newConcurrentMap(); /** * 注入所有實現(xiàn)了Strategy接口的Bean * * @param strategyMap the strategy map */ @Autowired public PushMessageStrategyContext(Map<String, PushMessageStrategy> strategyMap) { strategyMap.forEach(this.strategyMap::put); } /** * 策略監(jiān)聽redis所有過期key * * @param beanName the bean name * @param redisInvalidKey the redis invalid key * @return */ public void handleRedisExpireKey(String beanName, String redisInvalidKey) { if (Objects.nonNull(strategyMap.get(beanName))) { strategyMap.get(beanName).handleRedisExpireKey(redisInvalidKey); } else { log.warn("找不到對應(yīng)的策略:{}", strategyMap.get(beanName)); } } }
public interface PushMessageStrategy { void handleRedisExpireKey(String redisInvalidKey); }
大家在使用的時候,可以針對自己不同的場景,實現(xiàn)PushMessageStrategy 接口,重寫handleRedisExpireKey方法。
在方法中處理不同的邏輯。
首先,在redis’中有key失效過期時,AgentOrderRedisKeyExpiredListener 監(jiān)聽到redis實現(xiàn),會執(zhí)行doHandleMessage方法,方法中會調(diào)用PushMessageStrategyContext 的獲取不同策略的Bean,進行業(yè)務(wù)操作。
至此,監(jiān)聽redis中key失效完成。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java reservedcodecachesize虛擬機參數(shù)案例詳解
這篇文章主要介紹了Java reservedcodecachesize虛擬機參數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08idea2019.1.4 鼠標放到方法上顯示注解的實現(xiàn)操作
這篇文章主要介紹了idea2019.1.4 鼠標放到方法上顯示注解的實現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Spring Boot 自定義 Shiro 過濾器無法使用 @Autowired問題及解決方法
這篇文章主要介紹了Spring Boot 自定義 Shiro 過濾器無法使用 @Autowired問題及解決方法 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06SpringBoot快速設(shè)置攔截器并實現(xiàn)權(quán)限驗證的方法
本篇文章主要介紹了SpringBoot快速設(shè)置攔截器并實現(xiàn)權(quán)限驗證的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01解決FeignClient發(fā)送post請求異常的問題
這篇文章主要介紹了FeignClient發(fā)送post請求異常的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Alibaba?SpringCloud集成Nacos、openFeign實現(xiàn)負載均衡的解決方案
Spring?Cloud?Alibaba?致力于提供微服務(wù)開發(fā)的一站式解決方案,此項目包含開發(fā)分布式應(yīng)用微服務(wù)的必需組件,這篇文章主要介紹了Alibaba?SpringCloud集成Nacos、openFeign實現(xiàn)負載均衡,需要的朋友可以參考下2024-05-05Java使用ffmpeg和mencoder實現(xiàn)視頻轉(zhuǎn)碼
這篇文章主要為大家詳細介紹了Java使用ffmpeg和mencoder實現(xiàn)視頻轉(zhuǎn)碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12