介绍前言
配置复述。confg
与SpringBoot进行集成
这篇文章将为大家详细讲解有关复述中如何实现订单自动过期功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
前言
用户下单后,规定XX分钟后自动设置为“已过期”,不能再发起支付。项目类似此类“过期“的需求,笔者提供一种使用复述的解决思路,结合复述的订阅,发布和键空间通知机制(用于通知)进行实现。
配置复述。confg
notify-keyspace-events选项默认是不启用,改为notify-keyspace-events“前任”。重启生效,索引位我的库,每当有过期的元素被删除时,向* * keyspace@:过期* *频道发送通知。
E表示键事件通知,所有通知以__keyevent@__:为到期前缀;
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 才能;配置; ,} , ,@ bean ,public JedisPool  jedisPool (), { JedisPoolConfig 才能;config =, this.jedisPoolConfig (); JedisPool 才能;JedisPool =, new JedisPool (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 =皃aymentListener") public class  PaymentListener implements MessageListener  { ,@Override ,@ transactional ,public void  onMessage (Message 消息,byte[],模式),{//,才能过期事件处理流程 ,} }
③配置订阅对象
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") null null null null null null null null null null null null null null null null null null null null复述中如何实现订单自动过期功能