久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

redis怎樣實現訂單自動過期功能

113次閱讀
沒有評論

共計 4456 個字符,預計需要花費 12 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

這篇文章主要介紹了 redis 怎樣實現訂單自動過期功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。

文章背景

我們的目的是在用戶下單后,規定指定時間后自動將訂單設置為“已過期”,不能再發起支付。

思路:

結合 Redis 的訂閱、發布和鍵空間通知機制(Keyspace Notifications)進行實現。

配置 redis.confg

notify-keyspace-events 選項默認是不啟用,改為 notify-keyspace-events“Ex”。重啟生效,索引位 i 的庫,每當有過期的元素被刪除時,向 **keyspace@:expired** 頻道發送通知。
E 表示鍵事件通知,所有通知以__keyevent@__:expired 為前綴;
x 表示過期事件,每當有過期被刪除時發送。

與 SpringBoot 進行集成

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

2、注冊監聽器

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

3、配置訂閱對象

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);
 //  監聽 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) 方法。

感謝你能夠認真閱讀完這篇文章,希望丸趣 TV 小編分享的“redis 怎樣實現訂單自動過期功能”這篇文章對大家有幫助,同時也希望大家多多支持丸趣 TV,關注丸趣 TV 行業資訊頻道,更多相關知識等著你來學習!

向 AI 問一下細節

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-16發表,共計4456字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 乌兰察布市| 诸城市| 安顺市| 满城县| 乐亭县| 嘉兴市| 鱼台县| 安庆市| 钟祥市| 锡林浩特市| 新余市| 滦南县| 雅江县| 龙岩市| 双江| 上犹县| 桑日县| 福贡县| 石首市| 凤山市| 穆棱市| 四会市| 涿州市| 响水县| 防城港市| 萨迦县| 永昌县| 昌黎县| 道真| 景谷| 肃宁县| 维西| 古浪县| 马公市| 平果县| 石阡县| 上饶市| 靖远县| 休宁县| 织金县| 孟连|