亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Redis過期監(jiān)聽機制,訂單超時自動取消方式

 更新時間:2024年05月27日 10:18:06   作者:絕對沒精神  
這篇文章主要介紹了Redis過期監(jiān)聽機制,訂單超時自動取消方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Redis過期監(jiān)聽機制 (Windows) 

一、功能介紹

1. redis過期監(jiān)聽:當(dāng)數(shù)據(jù)設(shè)置了過期時間,redis會根據(jù)某些機制去時時監(jiān)聽過期的數(shù)據(jù) 

2. 應(yīng)用場景: 商城中未支付過期的訂單、通知(當(dāng)然也可以用redis的延遲)等等 

3. 本篇文章只限于Windows,Linux配置差不多,只是操作系統(tǒng)不同

二、redis過期監(jiān)聽的配置

1. 在redis安裝的目錄下找到redis.windows.conf文件

2. 編輯redis.windows.conf找到配置文件中notify-keyspace-events " " 的值, 

修改為notify-keyspace-events Ex(如圖下)

3. 關(guān)閉redis啟動窗口,在redis安裝的目錄下找到start.bat重啟redis (如圖下)

4. 在SpringBoot集成使用

  • 1、引入redis相關(guān)依賴(如圖下)
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

  • 2、創(chuàng)建配置類RedisListenerConfig
import org.springframework.beans.factory.annotation.Autowired;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @date 2023-02-21
 * @author LIZAN
 */
@Configuration
public class RedisListenerConfig {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 處理亂碼
     * @return
     */
    @Bean
    public RedisTemplate redisTemplateInit() {

        // key序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //val實例化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}
  • 3、繼承KeyExpirationEventMessageListener創(chuàng)建redis過期事件的監(jiān)聽類,實現(xiàn)onMessage方法接收過去redis數(shù)據(jù)
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @author LiZan
 * @version 1.0
 * @date 2023/2/21 14:09
 */
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 針對redis數(shù)據(jù)失效事件,進行數(shù)據(jù)處理
     * @param message 失效的key
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("過期redis數(shù)據(jù):" + message.toString());
        try {
            String key = message.toString();
            //從失效key中篩選代表訂單失效的key
            String orderWithKey = "order_";
            if (null !=  key && orderWithKey.startsWith(key)) {
                        log.info("訂單號為【" + 123456 + "】超時未支付-自動修改為已取消狀態(tài)");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【修改支付訂單過期狀態(tài)異?!浚? + e.getMessage());
        }
    }
}
  • 4、測試Redis過期監(jiān)聽,在redis安裝的目錄下找到redis-cli.exe 打開后執(zhí)行redis語法寫入五秒后過期的測試數(shù)據(jù),SET order_no123213 123 EX 5

  • 5、數(shù)據(jù)過期后,進入Redis過期監(jiān)聽方法,打印過期數(shù)據(jù)從而實現(xiàn)業(yè)務(wù)

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • redis?key鍵過期刪除策略及淘汰機制探究

    redis?key鍵過期刪除策略及淘汰機制探究

    這篇文章主要為大家介紹了redis?key鍵過期刪除策略及淘汰機制探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • redis解決高并發(fā)看門狗策略的實現(xiàn)

    redis解決高并發(fā)看門狗策略的實現(xiàn)

    本文主要介紹了redis解決高并發(fā)看門狗策略的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • 一文帶你了解Redis怎么啟動以及使用

    一文帶你了解Redis怎么啟動以及使用

    對于Redis我們一般會使用到三種啟動方式:直接啟動、指定配置文件啟動、開機自啟動,下面這篇文章主要給大家介紹了關(guān)于Redis怎么啟動以及使用的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Redis拓展之定時消息通知實現(xiàn)詳解

    Redis拓展之定時消息通知實現(xiàn)詳解

    這篇文章主要為大家介紹了Redis拓展之定時消息通知實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • 利用Redis實現(xiàn)訪問次數(shù)限流的方法詳解

    利用Redis實現(xiàn)訪問次數(shù)限流的方法詳解

    這篇文章主要給大家介紹了關(guān)于如何利用Redis實現(xiàn)訪問次數(shù)限流的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02
  • redis列表類型_動力節(jié)點Java學(xué)院整理

    redis列表類型_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了redis列表類型的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Redis中TYPE命令的具體使用

    Redis中TYPE命令的具體使用

    本文主要介紹了Redis中TYPE命令的具體使用,它用于返回存儲在指定鍵中的值的數(shù)據(jù)類型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Redis實現(xiàn)接口防抖的示例代碼

    Redis實現(xiàn)接口防抖的示例代碼

    本文介紹了一種通過AOP、自定義注解和Redis實現(xiàn)的接口防抖技術(shù),這種方法能有效避免因網(wǎng)絡(luò)波動等原因短時間內(nèi)發(fā)送多個請求導(dǎo)致的數(shù)據(jù)重復(fù)添加問題,感興趣的可以了解一下
    2024-10-10
  • 基于redis實現(xiàn)世界杯排行榜功能項目實戰(zhàn)

    基于redis實現(xiàn)世界杯排行榜功能項目實戰(zhàn)

    前段時間,做了一個世界杯競猜積分排行榜。對世界杯64場球賽勝負(fù)平進行猜測,猜對+1分,錯誤+0分,一人一場只能猜一次。下面通過本文給大家分享基于redis實現(xiàn)世界杯排行榜功能項目實戰(zhàn),感興趣的朋友一起看看吧
    2018-10-10
  • redis中隊列消息實現(xiàn)應(yīng)用解耦的方法

    redis中隊列消息實現(xiàn)應(yīng)用解耦的方法

    這篇文章主要給大家介紹了關(guān)于redis中隊列消息實現(xiàn)應(yīng)用解耦的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09

最新評論