監聽redis過期的key
一:需要監聽過期的redis keys
在redis.conf 裡新增 notify-keyspace-events "Ex" ,放到配置引數的最後。
二:不需要賬號密碼訪問
改變redis.conf 裡的protected-mode 的屬性值為 no
三:去掉繫結的地址
在redis.conf 裡登出掉 bind 127.0.0.1
./redis-service ../redis.conf &
客戶端執行redis 命令。 psubscribe [email protected]*__:expiredpubsub配置:
@Configuration
@Import( value = ServiceApplication.class )
public class PubsubConfiguration {
/* @Autowired
private RedisClient redisClient;*/
@Bean
MessageListenerAdapter messageListener() {
return new MessageListenerAdapter( new RedisMessageListener() );
}
@Autowired
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory ) {
final RedisMessageListenerContainer container =
new RedisMessageListenerContainer();
container.setConnectionFactory( connectionFactory );
container.addMessageListener( messageListener(), new ChannelTopic( " [email protected]__:expired" ) );
return container;
}
/* @Bean
public MessageListener listener() {
return new RedisMessageListener();
}*/
}
springboot 啟動類
@SpringBootApplication @ComponentScan public class ServiceApplication implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(ServiceApplication.class); @Override public void run(String... strings) throws Exception { } public static void main(String[] args) throws Exception { ApplicationContext ctx = SpringApplication.run(ServiceApplication.class, args); SpringContextUtil.setApplicationContext(ctx); logger.info("service start success ....."); } }
監聽類實現:
public class RedisMessageListener implements MessageListener { protected Logger logger = LoggerFactory.getLogger(RedisMessageListener.class); @Autowired private BookTableBo bookTableBo; @Autowired private CalledRecordBo calledRecordBo; @Autowired private MqService mqService; @Override public void onMessage(Message message, byte[] pattern) { String key = message.toString(); try { System.out.println( "Message received: " + message.toString() ); logger.info( "Message received: " + message.toString() ); if(StringUtils.isNotBlank(key) && key.startsWith(RedisUtils.TABLE_PREFIX)){ String[] tKey = key.split("_"); if(tKey.length==7){ String traceID = UUID.randomUUID().toString(); Integer groupID = Integer.parseInt(tKey[2]); Integer shopID = Integer.parseInt(tKey[3]); long mealDate = Long.parseLong(tKey[4]); Integer mealTimeTypeID = Integer.parseInt(tKey[5]); Integer tableID = Integer.parseInt(tKey[6]); String redisKey = new StringBuilder() .append(groupID) .append("_") .append(shopID) .append("_") .append(mealDate) .append("_") .append(mealTimeTypeID) .toString(); if(null == bookTableBo){ bookTableBo =(BookTableBo) SpringContextUtil.getBean("bookTableBo"); } if(null == mqService){ mqService =(MqService) SpringContextUtil.getBean("mqService"); } DataSourceContextHolder.setDataSourceType(Constants.DB_ROUTEKEY_PREFIX_READ + groupID); List<Integer> userableUnLockTables = bookTableBo.userableUnLockTable(groupID, shopID, tableID, traceID, redisKey, mealDate, mealTimeTypeID); List<MealTimeTableStatusMqVO> mealTimeTableStatusMqBeans = new ArrayList<>(); for (Integer one:userableUnLockTables){ mealTimeTableStatusMqBeans.add(MqUtils.buildMealTimeTableStatusMqBean(one, OrderStatusEnum.FREE.getStatus(),0L,0L,0,"",0,"",0)); } mqService.sendMessToShop(shopID, MqUtils.buildUnTakeUpTableMq(traceID,mealDate,mealTimeTypeID,mealTimeTableStatusMqBeans)); mqService.sendTableStatusChangeMq(groupID,shopID,mealDate,mealTimeTypeID,userableUnLockTables,null); } } }catch (Exception e){ logger.error("handler expire key error"+key,e); } } }
相關推薦
SpringBoot2監聽redis過期的key
1、修改Redis配置 把 # notify-keyspace-events Ex 前面的註釋去掉,然後重啟redis 2、pom檔案新增 <dependency> <groupId>org.
監聽redis過期的key
一:需要監聽過期的redis keys 在redis.conf 裡新增 notify-keyspace-events "Ex" ,放到配置引數的最後。二:不需要賬號密碼訪問 改變redis.conf 裡的protected-mode 的屬性值為 no三:去掉繫
php監聽redis key失效觸發回撥事件
一、需求分析: 1、設定了生命時間的key,過期的時候能不能提示,能夠監聽過期的key? 2、怎樣用redis實現定時任務? 二、應用場景: 在我們程式中經常會有需要定時執行的程式,比如:商品下單後半小時內不支付自動撤單等等。 最簡單粗暴的辦法,就是寫一個程式,讓它定時執行,
【Redis系列】Spring boot實現監聽Redis key失效事件
> talk is cheap, show me the code. ## 一、開啟Redis key過期提醒 - 方式二:修改配置檔案 `redis.conf` ``` # 預設 notify-keyspace-events "" notify-keyspace-events Ex
redis過期key刪除
-- bsp his notify initial nts thread get pub LZ一開始配置到啟動類裏面,結果出現了主線程阻塞的情況。 如下是流程: 首先修改配置文件redis.conf中的:notify-keyspace-events Ex,默認為noti
【大廠面試02期】Redis過期key是怎麼樣清理的?
> 【大廠面試02期】Redis過期key是怎麼樣清理的? 在Redis中,對於過期key的清理主要有惰性清除,定時清理,記憶體不夠時清理三種方法,下面我們就來具體看看這三種清理方法。 ### (1)惰性清除 在訪問key時,如果發現key已經過期,那麼會將key刪除。 ### (2)定時清理
redis監聽key過期
檢視redis版本: redis-server -v redis-server --version 在window下,下載redis:https://github.com/MicrosoftArchive/redis/releases linux下可以直接在官網下載即可:ht
Redis實現key過期監聽,並操作redis的多個數據庫,整合到SpringBoot
最近來了個新的需求,需要使用定時器完成,本想以為用個@Scheduled()就輕易搞定的,詳細瞭解後,事情卻並沒有這麼簡單......。所以接到需求後,需要找產品明確明確再次明確,才開工,不然的話你本以為做好的工作卻是一場空。 業務場景邏輯解析:第一個請求進來,需要把請求引
springboot redis 監聽過期key值事件
redis 中的key值過期後,觸發通知事件 1、建立springboot工程,建立監聽類 maven配置 <dependencies> <dependency> <groupId>org.springfr
Redis之監聽失效KEY
SpringConfig檔案,配置listenerContainer,redisKeyExpirationListener兩個bean package com.yj.config; import org.springframework.context.annotation.Bean; im
JAVA實現redis超時失效key 的監聽觸發
過期事件通過Redis的訂閱與釋出功能(pub/sub)來進行分發。 而對超時的監聽呢,並不需要自己釋出,只有修改配置檔案redis.conf中的:notify-keyspace-events Ex,
java中的key事件監聽機制
com java.awt imp package 時間 ext javax .get pri package com.at221; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; im
redis 下key的過期時間詳解 :expire
編寫 抽樣 通過 字符串 n) 開發包 有序集合 memcache jedis Redis是一個開源的Key-Value數據緩存,和Memcached類似。 Redis多種類型的value,包括string(字符串)、list(鏈表)、set(集合)、zset(sort
redis事件監聽及在訂單系統中的使用
https://blog.csdn.net/qq_37334135/article/details/77717248 通常在網上買好物品,或者說手機掃碼後,點選付款,這時就會向後臺傳送請求,生成訂單資訊,以及夠買商品的資訊存入到資料庫對應的表比如:訂單表和商品銷售表,或者還有物流資訊表等。簡
Python和Redis實現訂單監聽,語音播報
1.MP3格式的音訊檔案 如 audio.mp3 2.建立虛禮環境 建立虛擬環境 安裝:pip install virtualenv 建立:virtualenv venv 啟用:venv\Scripts\activate 3.安裝所需擴充套件 pip install playso
Redis中刪除過期Key的三種策略
專案中有個介面要頻繁呼叫查詢資料庫中的資料,為了降低資料庫的壓力,所以把一部分記錄先快取在redis中,對redis中的資料設定了期限。今天無意間發現一個問題,使用dbsize查詢出來的數量,比實際快取量要高一部分。用 redis-cli keys '*'|wc -l 1 獲取到的資料和實際情
redis判斷key是否存在(過期)的幾種方式
exist命令 EXISTS key 檢查給定 key 是否存在。 可用版本:>= 1.0.0 時間複雜度:O(1) 返回值: 若 key 存在,返回 1
spring session redis 實現叢集session共享,SessionListener監聽生效
pom主要配置 <properties> <spring.version>5.0.3.RELEASE</spring.version> <commons-lang.version
redis設定key過期時間
redis是一個廣泛應用的key-value型記憶體資料庫,和memecached一樣,key是可以被設定生存週期的。 redis設定key的過期時間非常簡單: SETEX mykey 10 "Hello" 這樣就給mykey設定了10秒的生存週期。 但是當你使用了red
解決使用tomcat-redis-session-manager httpSessionLinster監聽功能失效問題
使用tomcat-redis-session-manager做session redis化有一個坑就是,程式碼tomcat-redis-session-manager本身對session的生命週期控制使用了redis的exprie看看程式碼。。 RedisSessionMa