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

Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼

 更新時(shí)間:2021年05月08日 11:51:40   作者:SvenJoe  
這篇文章主要介紹了Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

用戶下單后,規(guī)定XX分鐘后自動(dòng)設(shè)置為“已過(guò)期”,不能再發(fā)起支付。項(xiàng)目類似此類"過(guò)期"的需求,筆者提供一種使用Redis的解決思路,結(jié)合Redis的訂閱、發(fā)布和鍵空間通知機(jī)制(Keyspace Notifications)進(jìn)行實(shí)現(xiàn)。

配置redis.confg

notify-keyspace-events選項(xiàng)默認(rèn)是不啟用,改為notify-keyspace-events “Ex”。重啟生效,索引位i的庫(kù),每當(dāng)有過(guò)期的元素被刪除時(shí),向**keyspace@:expired**頻道發(fā)送通知。
E表示鍵事件通知,所有通知以__keyevent@__:expired為前綴;
x表示過(guò)期事件,每當(dāng)有過(guò)期被刪除時(shí)發(fā)送。

與SpringBoot進(jìn)行集成

①注冊(cè)JedisConnectionFactory

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
 
 @Value("${redis.pool.maxTotal}")
 private Integer maxTotal;
 
 @Value("${redis.pool.minIdle}")
 private Integer minIdle;
 
 @Value("${redis.pool.maxIdle}")
 private Integer maxIdle;
 
 @Value("${redis.pool.maxWaitMillis}")
 private Integer maxWaitMillis;
 
 @Value("${redis.url}")
 private String redisUrl;
 
 @Value("${redis.port}")
 private Integer redisPort;
 
 @Value("${redis.timeout}")
 private Integer redisTimeout;
 
 @Value("${redis.password}")
 private String redisPassword;
 
 @Value("${redis.db.payment}")
 private Integer paymentDataBase;
 
 private JedisPoolConfig jedisPoolConfig() {
  JedisPoolConfig config = new JedisPoolConfig();
  config.setMaxTotal(maxTotal);
  config.setMinIdle(minIdle);
  config.setMaxIdle(maxIdle);
  config.setMaxWaitMillis(maxWaitMillis);
  return config;
 }
 
 @Bean
 public JedisPool jedisPool() {
  JedisPoolConfig config = this.jedisPoolConfig();
  JedisPool jedisPool = new JedisPool(config, redisUrl, redisPort, redisTimeout, redisPassword);
  return jedisPool;
 }
 
 @Bean(name = "jedisConnectionFactory")
 public JedisConnectionFactory jedisConnectionFactory() {
  RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
  redisStandaloneConfiguration.setDatabase(paymentDataBase);
  redisStandaloneConfiguration.setHostName(redisUrl);
  redisStandaloneConfiguration.setPassword(RedisPassword.of(redisPassword));
  redisStandaloneConfiguration.setPort(redisPort);

  return new JedisConnectionFactory(redisStandaloneConfiguration);
 }
}

②注冊(cè)監(jiān)聽器

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value ="paymentListener")
public class PaymentListener implements MessageListener {

 @Override
 @Transactional
 public void onMessage(Message message, byte[] pattern) {
  // 過(guò)期事件處理流程
 }
}

③配置訂閱對(duì)象

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@AutoConfigureAfter(value = RedisConfig.class)
public class PaymentListenerConfig {
 
 @Autowired
 @Qualifier(value = "paymentListener")
 private PaymentListener paymentListener;
 
 @Autowired
 @Qualifier(value = "paymentListener")
 private JedisConnectionFactory connectionFactory;
 
 @Value("${redis.db.payment}")
 private Integer paymentDataBase;
 
 @Bean
 RedisMessageListenerContainer redisMessageListenerContainer(MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 監(jiān)聽paymentDataBase 庫(kù)的過(guò)期事件
        String subscribeChannel = "__keyevent@" + paymentDataBase + "__:expired";
        container.addMessageListener(listenerAdapter, new PatternTopic(subscribeChannel));
        return container;
 }
 
 @Bean
    MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(paymentListener);
    }
}

paymentDataBase 庫(kù)元素過(guò)期后就會(huì)跳入PaymentListener 的onMessage(Message message, byte[] pattern)方法。

到此這篇關(guān)于Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼的文章就介紹到這了,更多相關(guān)Redis 訂單自動(dòng)過(guò)期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • redis過(guò)期回調(diào)功能實(shí)現(xiàn)示例

    redis過(guò)期回調(diào)功能實(shí)現(xiàn)示例

    Redis提供了一種過(guò)期回調(diào)的機(jī)制,可以在某個(gè)鍵過(guò)期時(shí)觸發(fā)一個(gè)回調(diào)函數(shù),本文就來(lái)介紹一下redis過(guò)期回調(diào)功能實(shí)現(xiàn)示例,感興趣的可以了解一下
    2023-09-09
  • Redis學(xué)習(xí)教程之命令的執(zhí)行過(guò)程詳解

    Redis學(xué)習(xí)教程之命令的執(zhí)行過(guò)程詳解

    這篇文章主要給大家介紹了關(guān)于Redis學(xué)習(xí)教程之命令的執(zhí)行過(guò)程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • Redisson分布式鎖解鎖異常問(wèn)題

    Redisson分布式鎖解鎖異常問(wèn)題

    文章主要描述了在使用Redisson進(jìn)行分布式鎖操作時(shí),遇到的`IllegalMonitorStateException`異常,通過(guò)分析報(bào)錯(cuò)信息,作者發(fā)現(xiàn)了問(wèn)題的根源在于線程嘗試解鎖但未按順序執(zhí)行,作者提出了兩種解決方案:在解鎖前增加判斷,確保線程已獲得鎖
    2024-12-12
  • Redis報(bào)錯(cuò)“NOAUTH Authentication required”兩種解決方案

    Redis報(bào)錯(cuò)“NOAUTH Authentication required”兩種解決方案

    Redis提供了一個(gè)命令行工具redis-cli,它允許你直接連接到Redis服務(wù)器,如果你知道Redis服務(wù)器的密碼,你可以在連接時(shí)直接提供它,本文給大家介紹連接了Redis報(bào)錯(cuò)“NOAUTH Authentication required”兩種解決方案
    2024-05-05
  • Redis如何實(shí)現(xiàn)分布式鎖

    Redis如何實(shí)現(xiàn)分布式鎖

    相信大家對(duì)鎖已經(jīng)不陌生了,本文主要介紹了Redis如何實(shí)現(xiàn)分布式鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 通過(guò)kubesphere部署redis的方法

    通過(guò)kubesphere部署redis的方法

    這篇文章主要介紹了通過(guò)kubesphere部署redis的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Redis 命令的詳解及簡(jiǎn)單實(shí)例

    Redis 命令的詳解及簡(jiǎn)單實(shí)例

    這篇文章主要介紹了Redis 命令的詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,這里提供基礎(chǔ)語(yǔ)法及使用實(shí)例,需要的朋友可以參考下
    2017-08-08
  • 基于redis分布式鎖實(shí)現(xiàn)秒殺功能

    基于redis分布式鎖實(shí)現(xiàn)秒殺功能

    這篇文章主要為大家詳細(xì)介紹了基于redis分布式鎖實(shí)現(xiàn)秒殺功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Redis中緩存預(yù)熱與緩存穿透解決方案

    Redis中緩存預(yù)熱與緩存穿透解決方案

    Redis緩存預(yù)熱與緩存穿透是Redis緩存使用中的兩個(gè)重要概念,文章首先介紹了Redis緩存預(yù)熱和緩存穿透的基本概念,然后詳細(xì)闡述了它們的產(chǎn)生原因和解決方案,感興趣的可以了解一下
    2023-12-12
  • Redis的數(shù)據(jù)過(guò)期清除策略實(shí)現(xiàn)

    Redis的數(shù)據(jù)過(guò)期清除策略實(shí)現(xiàn)

    Redis實(shí)現(xiàn)了數(shù)據(jù)過(guò)期清除策略,本文將深入解析Redis的數(shù)據(jù)過(guò)期清除策略,包括過(guò)期鍵的刪除方式、清除策略的選擇以及相關(guān)配置參數(shù)的介紹,感興趣的可以了解一下
    2024-05-05

最新評(píng)論

吉林市| 泽普县| 从江县| 阿坝| 法库县| 班玛县| 泾阳县| 澄迈县| 和林格尔县| 石泉县| 巴林右旗| 中卫市| 临武县| 莆田市| 翼城县| 靖远县| 黑龙江省| 九龙县| 固阳县| 阳山县| 彭阳县| 栾城县| 东阿县| 遵义县| 义马市| 象州县| 滨海县| 临泉县| 广德县| 天津市| 石嘴山市| 大宁县| 南澳县| 中超| 古浪县| 汉阴县| 广汉市| 海丰县| 巴彦淖尔市| 郓城县| 崇仁县|