您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Redis中如何實現訂單自動過期功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
用戶下單后,規定XX分鐘后自動設置為“已過期”,不能再發起支付。項目類似此類"過期"的需求,筆者提供一種使用Redis的解決思路,結合Redis的訂閱、發布和鍵空間通知機制(Keyspace Notifications)進行實現。
notify-keyspace-events選項默認是不啟用,改為notify-keyspace-events “Ex”。重啟生效,索引位i的庫,每當有過期的元素被刪除時,向**keyspace@:expired**頻道發送通知。
E表示鍵事件通知,所有通知以__keyevent@__:expired為前綴;
x表示過期事件,每當有過期被刪除時發送。
①注冊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中如何實現訂單自動過期功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。