springboot+redis過(guò)期事件監(jiān)聽(tīng)實(shí)現(xiàn)過(guò)程解析
1 修改 redis.conf配置文件:
K Keyspace events, published with keyspace@ prefix事件E Keyevent events, published with keyevent@ prefixg Generic commands (non-type specific) like DEL, EXPIRE, RENAME, …$ String commandsl List commandss Set commandsh Hash commandsz Sorted set commandsx Expired events (events generated every time a key expires)e Evicted events (events generated when a key is evicted for maxmemory)A Alias for g$lshzxe, so that the “AKE” string means all the events.redis.conf 的默認(rèn)的配置是:notify-keyspace-events ''我們需要改為:notify-keyspace-events Ex即對(duì)應(yīng)上面的鍵的過(guò)期事件。修改玩這個(gè)重啟一下redis
2 客戶端來(lái)監(jiān)聽(tīng)redis的過(guò)期事件:
@Configurationpublic class RedisListenerConfig { @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; }}
3.書(shū)寫一個(gè)監(jiān)聽(tīng)器
@Slf4j@Componentpublic class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); log.info('expiredKey========='+expiredKey); }
4.查詢方法中隨便加了兩個(gè)表中的不同id,一個(gè)30s,一個(gè)27s。
redisUtil.set('UserId'+user.get(0).getId(),user.get(0).getId(),30);redisUtil.set('UserInfoId'+userInfo.get(0).getId(),userInfo.get(0).getId(),27);
控制臺(tái)輸出:
需要注意的是:
過(guò)期監(jiān)聽(tīng)消息中返回的是,過(guò)期的鍵的key值,是沒(méi)有返回value的
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML2. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析3. python excel和yaml文件的讀取封裝4. python3實(shí)現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)5. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊6. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決7. Django 權(quán)限管理(permissions)與用戶組(group)詳解8. App啟動(dòng)優(yōu)化-Android性能優(yōu)化9. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例10. python中PyQuery庫(kù)用法分享
