最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot使用Redis單機(jī)版過期鍵監(jiān)聽事件的實(shí)現(xiàn)示例

 更新時(shí)間:2024年07月22日 11:14:09   作者:師小師  
在緩存的使用場(chǎng)景中經(jīng)常需要使用到過期事件,本文主要介紹了SpringBoot使用Redis單機(jī)版過期鍵監(jiān)聽事件的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Redis單機(jī)版

SpringBoot版本:2.3.6.RELEASE

依賴

<!-- redis依賴 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- commons-pool2連接池 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

application.yml 配置信息

spring:
  # redis 配置
  redis:
    # 地址
    host: localhost
    # 端口,默認(rèn)為6379
    port: 6379
    # 數(shù)據(jù)庫(kù)索引
    database: 0
    # 密碼
    password:
    # 連接超時(shí)時(shí)間
    timeout: 10s
    lettuce:
      pool:
        # 連接池中的最小空閑連接
        min-idle: 0
        # 連接池中的最大空閑連接
        max-idle: 8
        # 連接池的最大數(shù)據(jù)庫(kù)連接數(shù)
        max-active: 8
        # #連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
        max-wait: -1ms

redis配置文件

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis序列化規(guī)則配置類
 */
@Configuration
public class RedisConfig {

    /**
     * 自定義序列化機(jī)制
     *
     * @param connectionFactory
     * @return
     */
    @Bean
    @SuppressWarnings(value = {"unchecked", "rawtypes"})
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(connectionFactory);
        // Json序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        // String 的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    // redis 監(jiān)聽事件
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }

}

redis監(jiān)聽配置信息

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
        log.info("過期事件,啟動(dòng)監(jiān)聽......");
    }

    /**
     * Redis失效事件 key
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expireKey = message.toString();
        System.out.println("過期事件監(jiān)聽鍵:" + expireKey);
        // 獲取訂單編號(hào)
        if (expireKey.startsWith("order:")) {
            // 截取訂單編號(hào)
            final String str = expireKey.substring(expireKey.lastIndexOf(":") + 1);
            System.out.println(str);
            System.out.println("轉(zhuǎn)換后訂單編號(hào):" + Long.valueOf(str));
            // TODO 業(yè)務(wù)處理邏輯
        }
    }
}

打開 redis 客戶端添加 key:SETEX "order:20220824170501" 20 "20220824170501" 20秒后過期

查看控制臺(tái)輸出內(nèi)容:

在這里插入圖片描述

到此這篇關(guān)于SpringBoot使用Redis單機(jī)版過期鍵監(jiān)聽事件的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot Redis過期鍵監(jiān)聽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置及獲取非確定名稱配置方法

    SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置及獲取非確定名稱配置方法

    在SpringBoot項(xiàng)目中,使用@ConfigurationProperties注解可獲取統(tǒng)一前綴的配置,具體做法是創(chuàng)建配置類,使用prefix屬性指定配置的前綴,本文給大家介紹SpringBoot項(xiàng)目獲取統(tǒng)一前綴配置以及獲取非確定名稱配置方法,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • SpringBoot集成Mybatis-Plus多租戶架構(gòu)實(shí)現(xiàn)

    SpringBoot集成Mybatis-Plus多租戶架構(gòu)實(shí)現(xiàn)

    本文主要介紹了SpringBoot集成Mybatis-Plus多租戶架構(gòu)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • SpringBoot兩種方式刷新配置信息

    SpringBoot兩種方式刷新配置信息

    這篇文章主要介紹了SpringBoot兩種方式刷新配置信息,一種是@?ConfigurationProperties?不能自動(dòng)刷新,需要手動(dòng)調(diào)用contextRefresher.refresh()方法來(lái)刷新配置,第二種方法可以嘗試下,需要的朋友可以參考下
    2023-08-08
  • 最新評(píng)論

    卢湾区| 抚顺县| 独山县| 民和| 平凉市| 武鸣县| 德钦县| 泸溪县| 衡阳市| 海林市| 桃江县| 渝北区| 崇礼县| 遂溪县| 台中市| 将乐县| 襄汾县| 崇阳县| 三都| 潍坊市| 萨嘎县| 遵化市| 汾阳市| 石狮市| 阳信县| 鄂州市| 太康县| 海晏县| 铁力市| 青浦区| 白沙| 富平县| 湘潭市| 岳阳市| 佛教| 阿合奇县| 海口市| 宜昌市| 肃南| 临沧市| 儋州市|