主頁(yè) > 知識(shí)庫(kù) > Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼

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

熱門(mén)標(biāo)簽:日本中國(guó)地圖標(biāo)注 貴州電銷(xiāo)卡外呼系統(tǒng) 北京400電話辦理收費(fèi)標(biāo)準(zhǔn) 鄭州人工智能電銷(xiāo)機(jī)器人系統(tǒng) 超呼電話機(jī)器人 十堰營(yíng)銷(xiāo)電銷(xiāo)機(jī)器人哪家便宜 山東外呼銷(xiāo)售系統(tǒng)招商 宿遷便宜外呼系統(tǒng)平臺(tái) 魔獸2青云地圖標(biāo)注

前言

用戶下單后,規(guī)定XX分鐘后自動(dòng)設(shè)置為“已過(guò)期”,不能再發(fā)起支付。項(xiàng)目類(lèi)似此類(lèi)"過(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)聽(tīng)器

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)聽(tīng)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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Redis中的數(shù)據(jù)過(guò)期策略詳解
  • Python操作Redis之設(shè)置key的過(guò)期時(shí)間實(shí)例代碼
  • Redis中鍵的過(guò)期刪除策略深入講解
  • springboot+redis過(guò)期事件監(jiān)聽(tīng)實(shí)現(xiàn)過(guò)程解析
  • Redis中鍵值過(guò)期操作示例詳解
  • Redis有效時(shí)間設(shè)置以及時(shí)間過(guò)期處理操作
  • spring boot+redis 監(jiān)聽(tīng)過(guò)期Key的操作方法
  • Redis 對(duì)過(guò)期數(shù)據(jù)的處理方法
  • python redis 批量設(shè)置過(guò)期key過(guò)程解析
  • SpringBoot如何整合redis實(shí)現(xiàn)過(guò)期key監(jiān)聽(tīng)事件

標(biāo)簽:江蘇 果洛 臺(tái)州 朝陽(yáng) 大慶 吉安 楊凌 北京

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼》,本文關(guān)鍵詞  Redis,實(shí)現(xiàn),訂單,自動(dòng),過(guò)期,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章