您好,登錄后才能下訂單哦!
這篇文章主要介紹了redis怎樣實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
文章背景
我們的目的是在用戶下單后,規(guī)定指定時(shí)間后自動(dòng)將訂單設(shè)置為“已過(guò)期”,不能再發(fā)起支付。
思路:
結(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)行集成
1、注冊(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); } }
2、注冊(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ò)期事件處理流程 } }
3、配置訂閱對(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)方法。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“redis怎樣實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。