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

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

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

前言

用戶下單后,規(guī)定XX分鐘后自動設(shè)置為“已過期”,不能再發(fā)起支付。項(xiàng)目類似此類"過期"的需求,筆者提供一種使用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的庫,每當(dāng)有過期的元素被刪除時,向**keyspace@:expired**頻道發(fā)送通知。
E表示鍵事件通知,所有通知以__keyevent@__:expired為前綴;
x表示過期事件,每當(dāng)有過期被刪除時發(fā)送。

與SpringBoot進(jìn)行集成

①注冊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);
 }
}

②注冊監(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) {
  // 過期事件處理流程
 }
}

③配置訂閱對象

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 庫的過期事件
        String subscribeChannel = "__keyevent@" + paymentDataBase + "__:expired";
        container.addMessageListener(listenerAdapter, new PatternTopic(subscribeChannel));
        return container;
 }
 
 @Bean
    MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(paymentListener);
    }
}

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

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

相關(guān)文章

  • Redis高效率原因及數(shù)據(jù)結(jié)構(gòu)分析

    Redis高效率原因及數(shù)據(jù)結(jié)構(gòu)分析

    這篇文章主要為大家詳細(xì)的介紹了Redis高效的原因以及分析了Redis高效的數(shù)據(jù)結(jié)構(gòu),有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • Redisson延時隊(duì)列RedissonDelayed的具體使用

    Redisson延時隊(duì)列RedissonDelayed的具體使用

    定時調(diào)度基本是每個項(xiàng)目都會遇到的業(yè)務(wù)場景,一般地,都會通過任務(wù)調(diào)度工具執(zhí)行定時任務(wù)完成,但是會有一定的缺點(diǎn),本文主要介紹了Redisson延時隊(duì)列RedissonDelayed的具體使用,感興趣的可以了解一下
    2024-02-02
  • Redis集群Lettuce主從切換問題解決方案

    Redis集群Lettuce主從切換問題解決方案

    這篇文章主要為大家介紹了Redis集群Lettuce主從切換問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • ?Redis 串行生成順序編碼的方法實(shí)現(xiàn)

    ?Redis 串行生成順序編碼的方法實(shí)現(xiàn)

    本文主要介紹了?Redis 串行生成順序編碼的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過程

    Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過程

    這篇文章主要介紹了Redis序列化設(shè)置以及jetcache連接Redis序列化的設(shè)置過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Redis操作命令總結(jié)

    Redis操作命令總結(jié)

    這篇文章主要介紹了Redis操作命令總結(jié),本文講解了key pattern 查詢相應(yīng)的key、字符串類型的操作、鏈表操作、hashes類型及操作、集合結(jié)構(gòu)操作、有序集合、服務(wù)器相關(guān)命令等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • Redis緩存三大異常的處理方案梳理總結(jié)

    Redis緩存三大異常的處理方案梳理總結(jié)

    這篇文章主要介紹了Redis緩存三大異常的處理方案梳理總結(jié),緩存方式,在提高數(shù)據(jù)查詢效率、保護(hù)數(shù)據(jù)庫等方面起到了不可磨滅的作用,但實(shí)際應(yīng)用中,可能會出現(xiàn)一些Redis緩存異常的情況,下文對其方案總結(jié)需要的朋友可以參考一下
    2022-06-06
  • 如何使用Redis鎖處理并發(fā)問題詳解

    如何使用Redis鎖處理并發(fā)問題詳解

    這篇文章主要給大家介紹了關(guān)于如何使用Redis鎖處理并發(fā)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Redis不同數(shù)據(jù)類型使用場景代碼實(shí)例

    Redis不同數(shù)據(jù)類型使用場景代碼實(shí)例

    這篇文章主要介紹了Redis不同數(shù)據(jù)類型使用場景代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • Redis Template使用詳解示例教程

    Redis Template使用詳解示例教程

    RedisTemplate的底層通過RedisConnectionFactory對多種Redis驅(qū)動進(jìn)行集成,上層通過RedisOperations提供豐富的API,并結(jié)合Spring基于泛型的bean注入,為開發(fā)提供了極大的便利,這篇文章主要介紹了Redis Template使用詳解示例教程,需要的朋友可以參考下
    2023-11-11

最新評論