Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of automatic order expiration by redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces redis how to achieve automatic order expiration function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Background of the article

Our aim is to automatically set the order to "expired" after the specified time after the user has placed the order, so that payment can no longer be initiated.

Train of thought:

Combined with Redis subscription, publication and keyspace notification mechanism (Keyspace Notifications) to implement.

Configure redis.confg

The notify-keyspace-events option is not enabled by default, but is changed to notify-keyspace-events "Ex". If the restart takes effect, the library of bit I will send a notification to the * * keyspace@:expired** channel whenever an expired element is deleted.

E stands for key event notifications, all notifications are prefixed with _ _ keyevent@__:expired

X represents an expired event, which is sent whenever an expiration is deleted.

Integration with SpringBoot

1. Register 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;@Configurationpublic 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. Register the listener

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) {/ / expired event handling flow}}

3. Configure subscription object

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); / / listen to the expiration event of the paymentDataBase library String subscribeChannel = "_ _ keyevent@" + paymentDataBase + "_: expired"; container.addMessageListener (listenerAdapter, new PatternTopic (subscribeChannel)); return container } @ Bean MessageListenerAdapter listenerAdapter () {return new MessageListenerAdapter (paymentListener);}}

After the paymentDataBase library element expires, it jumps into the onMessage (Message message, byte [] pattern) method of PaymentListener.

Thank you for reading this article carefully. I hope the article "how to realize the automatic expiration of orders in redis" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report