欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2024年07月22日 11:14:09   作者:師小師  
在緩存的使用場(chǎng)景中經(jīng)常需要使用到過(guò)期事件,本文主要介紹了SpringBoot使用Redis單機(jī)版過(guò)期鍵監(jiān)聽(tīng)事件的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(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ù)值表示沒(méi)有限制)
        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)聽(tīng)事件
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }

}

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

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("過(guò)期事件,啟動(dòng)監(jiān)聽(tīng)......");
    }

    /**
     * Redis失效事件 key
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expireKey = message.toString();
        System.out.println("過(guò)期事件監(jiān)聽(tīng)鍵:" + 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ù)處理邏輯
        }
    }
}

打開(kāi) redis 客戶端添加 key:SETEX "order:20220824170501" 20 "20220824170501" 20秒后過(guò)期

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

在這里插入圖片描述

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

相關(guān)文章

  • 深入理解Java設(shè)計(jì)模式之策略模式

    深入理解Java設(shè)計(jì)模式之策略模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之策略模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • Sleuth+logback 設(shè)置traceid 及自定義信息方式

    Sleuth+logback 設(shè)置traceid 及自定義信息方式

    這篇文章主要介紹了Sleuth+logback 設(shè)置traceid 及自定義信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 關(guān)于IDEA中spring-cloud-starter-alibaba-nacos-discovery 無(wú)法引入問(wèn)題

    關(guān)于IDEA中spring-cloud-starter-alibaba-nacos-discovery 無(wú)法引入問(wèn)題

    這篇文章主要介紹了關(guān)于IDEA中spring-cloud-starter-alibaba-nacos-discovery 無(wú)法引入問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解

    SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解

    一般公司的核心業(yè)務(wù)代碼中,都會(huì)存在與數(shù)據(jù)庫(kù)、第三方通信的secret key等敏感信息,如果以明文的方式存儲(chǔ),一旦泄露,那將會(huì)給公司帶來(lái)巨大的損失。本篇文章通過(guò)講解:Springboot集成Jasypt對(duì)項(xiàng)目敏感信息進(jìn)行加密,提高系統(tǒng)的安全性
    2022-09-09
  • Java實(shí)體類(entity)作用說(shuō)明

    Java實(shí)體類(entity)作用說(shuō)明

    這篇文章主要介紹了Java實(shí)體類(entity)作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • 在IntelliJ IDEA中創(chuàng)建和運(yùn)行java/scala/spark程序的方法

    在IntelliJ IDEA中創(chuàng)建和運(yùn)行java/scala/spark程序的方法

    這篇文章主要介紹了在IntelliJ IDEA中創(chuàng)建和運(yùn)行java/scala/spark程序的教程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • springboot+vue制作后臺(tái)管理系統(tǒng)項(xiàng)目

    springboot+vue制作后臺(tái)管理系統(tǒng)項(xiàng)目

    本文詳細(xì)介紹了后臺(tái)管理使用springboot+vue制作,以分步驟、圖文的形式詳細(xì)講解,大家有需要的可以參考參考
    2021-08-08
  • IntelliJ IDEA中查看當(dāng)前類的所有繼承關(guān)系圖

    IntelliJ IDEA中查看當(dāng)前類的所有繼承關(guān)系圖

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中查看當(dāng)前類的所有繼承關(guān)系圖,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示功能(詳細(xì)步驟)

    SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示功能(詳細(xì)步驟)

    這篇文章主要介紹了SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示,通過(guò)數(shù)據(jù)庫(kù)設(shè)計(jì)、實(shí)現(xiàn)數(shù)據(jù)訪問(wèn)層、業(yè)務(wù)邏輯層和控制層的代碼編寫(xiě),以及前端頁(yè)面的開(kāi)發(fā),本文詳細(xì)地介紹了SpringBoot整合Echarts的實(shí)現(xiàn)步驟和代碼,需要的朋友可以參考下
    2023-05-05
  • java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例

    java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例

    這篇文章主要介紹了java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09

最新評(píng)論