SpringBoot Redis 鍵失效時監(jiān)聽

需求

在做有時效業(yè)務需求的時候, 比如我發(fā)起一個簽到, 2小時內(nèi)有效,過期就不能再簽到了.

做法

  1. 讀取數(shù)據(jù)的時候判斷結(jié)束時間是都小于當前時間, 是就過期, 不是就有效
  2. 設置定時任務, 讓任務去做這個事情, 我們只需要讀取狀態(tài)就可以了,
  3. 結(jié)合緩存Redis, 既能加快速度, 還有key失效的監(jiān)聽.

這里要描述Redis鍵失效的監(jiān)聽

代碼

  1. 修改Redis server的配置(redis.conf)
默認是: notify-keyspace-events "",修改成 notify-keyspace-events Ex;

配置Ex對應的意思如下: E: 鍵事件通知畏线,以__keysevent@<db>__為前綴 x:過期事件(每次key過期時生成)

# K    鍵空間通知镇匀,以__keyspace@<db>__為前綴  
# E    鍵事件通知,以__keysevent@<db>__為前綴  
# g    del , expipre , rename 等類型無關(guān)的通用命令的通知, ...  
# $    String命令  
# l    List命令  
# s    Set命令  
# h    Hash命令  
# z    有序集合命令  
# x    過期事件(每次key過期時生成)  
# e    驅(qū)逐事件(當key在內(nèi)存滿了被清除時生成)  
# A    g$lshzxe的別名客情,因此”AKE”意味著所有的事件  
  1. 編寫監(jiān)聽
public class MyKeyExpirationEventMessageListener extends KeyExpirationEventMessageListener{

    public MyKeyExpirationEventMessageListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
//      System.out.println(new String(message.getBody()));
//      System.out.println(new String(message.getChannel()));
//      System.out.println(new String(pattern));
//      super.onMessage(message, pattern);
    }
}
  1. SpringBoot 配置
package com.guangyiedu.clazz.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import com.guangyiedu.clazz.service.cache.MyKeyExpirationEventMessageListener;

import redis.clients.jedis.JedisPoolConfig;

/**
 * redis的配置
 * 
 * @author atrun
 *
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    @SuppressWarnings("rawtypes")
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis.pool")
    public JedisPoolConfig getRedisConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisConnectionFactory getConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        JedisPoolConfig config = getRedisConfig();
        factory.setPoolConfig(config);
        return factory;
    }

    @Bean
    public StringRedisTemplate redisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate(getConnectionFactory());
        return template;
    }
    
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(getConnectionFactory());
        return container;
    }
    
    @Bean
    public MyKeyExpirationEventMessageListener myKeyExpirationEventMessageListener (){
        MyKeyExpirationEventMessageListener listener = new MyKeyExpirationEventMessageListener(redisMessageListenerContainer());
        return listener;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子涧黄,更是在濱河造成了極大的恐慌漓帚,老刑警劉巖母债,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異尝抖,居然都是意外死亡毡们,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門昧辽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來衙熔,“玉大人,你說我怎么就攤上這事搅荞『炻龋” “怎么了框咙?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長痢甘。 經(jīng)常有香客問我喇嘱,道長,這世上最難降的妖魔是什么塞栅? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任者铜,我火速辦了婚禮,結(jié)果婚禮上放椰,老公的妹妹穿的比我還像新娘作烟。我一直安慰自己,他們只是感情好庄敛,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布俗壹。 她就那樣靜靜地躺著,像睡著了一般绷雏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上怖亭,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天涎显,我揣著相機與錄音,去河邊找鬼兴猩。 笑死期吓,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的倾芝。 我是一名探鬼主播讨勤,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼晨另!你這毒婦竟也來了潭千?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤借尿,失蹤者是張志新(化名)和其女友劉穎刨晴,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體路翻,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡狈癞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了茂契。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蝶桶。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖掉冶,靈堂內(nèi)的尸體忽然破棺而出真竖,到底是詐尸還是另有隱情儡蔓,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布疼邀,位于F島的核電站,受9級特大地震影響召锈,放射性物質(zhì)發(fā)生泄漏旁振。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一涨岁、第九天 我趴在偏房一處隱蔽的房頂上張望拐袜。 院中可真熱鬧,春花似錦梢薪、人聲如沸蹬铺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽甜攀。三九已至,卻和暖如春琐馆,著一層夾襖步出監(jiān)牢的瞬間规阀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工瘦麸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留谁撼,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓滋饲,卻偏偏與公主長得像厉碟,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子屠缭,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容