校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Redis實現訂單自動過期功能的示例代碼

Redis實現訂單自動過期功能的示例代碼

熱門標簽:日本中國地圖標注 貴州電銷卡外呼系統 北京400電話辦理收費標準 鄭州人工智能電銷機器人系統 超呼電話機器人 十堰營銷電銷機器人哪家便宜 山東外呼銷售系統招商 宿遷便宜外呼系統平臺 魔獸2青云地圖標注

前言

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

配置redis.confg

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

與SpringBoot進行集成

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

②注冊監聽器

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

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

您可能感興趣的文章:
  • Redis中的數據過期策略詳解
  • Python操作Redis之設置key的過期時間實例代碼
  • Redis中鍵的過期刪除策略深入講解
  • springboot+redis過期事件監聽實現過程解析
  • Redis中鍵值過期操作示例詳解
  • Redis有效時間設置以及時間過期處理操作
  • spring boot+redis 監聽過期Key的操作方法
  • Redis 對過期數據的處理方法
  • python redis 批量設置過期key過程解析
  • SpringBoot如何整合redis實現過期key監聽事件

標簽:江蘇 果洛 臺州 朝陽 大慶 吉安 楊凌 北京

巨人網絡通訊聲明:本文標題《Redis實現訂單自動過期功能的示例代碼》,本文關鍵詞  Redis,實現,訂單,自動,過期,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Redis實現訂單自動過期功能的示例代碼》相關的同類信息!
  • 本頁收集關于Redis實現訂單自動過期功能的示例代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 开阳县| 凤山市| 霍林郭勒市| 丹棱县| 婺源县| 新竹市| 淳化县| 牙克石市| 方城县| 犍为县| 阿瓦提县| 茌平县| 东辽县| 阜新市| 庆城县| 渝中区| 红河县| 迁西县| 巴青县| 松滋市| 米易县| 达拉特旗| 左贡县| 格尔木市| 新津县| 河东区| 简阳市| 恩施市| 临颍县| 资源县| 隆子县| 榆中县| 电白县| 汉中市| 会理县| 溧阳市| 通道| 保康县| 双鸭山市| 赤水市| 循化|