redis怎样实现订单自动过期功能
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要介绍了redis怎样实现订单自动过期功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。文章背景我们的目的是在用户下单后,规
千家信息网最后更新 2025年11月08日redis怎样实现订单自动过期功能
这篇文章主要介绍了redis怎样实现订单自动过期功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
文章背景
我们的目的是在用户下单后,规定指定时间后自动将订单设置为"已过期",不能再发起支付。
思路:
结合Redis的订阅、发布和键空间通知机制(Keyspace Notifications)进行实现。
配置redis.confg
notify-keyspace-events选项默认是不启用,改为notify-keyspace-events "Ex"。重启生效,索引位i的库,每当有过期的元素被删除时,向**keyspace@:expired**频道发送通知。
E表示键事件通知,所有通知以__keyevent@__:expired为前缀;
x表示过期事件,每当有过期被删除时发送。
与SpringBoot进行集成
1、注册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、注册监听器
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) { // 过期事件处理流程 }}3、配置订阅对象
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怎样实现订单自动过期功能"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
事件
篇文章
订单
功能
元素
频道
监听
订阅
配置
价值
兴趣
前缀
同时
对象
思路
文章
方法
时间
是在
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器怎么分2个区
服务器装16g还是装两根8g
gtrd数据库入口
向阳新华互联网科技学校
火山软件开发进销存源码
移动互联网科技综合服务中心
计算机网络安全相关的试题
网络安全服务系统代码
中央服务器幻灵石
app服务器安全注意事项
网络安全林鹏
暗宝用哪个软件开发
java判断数据库是否连通
pg数据库批量修改
互联网科技大国
杭州巨聚网络技术有限公司招聘
南京基础网络技术服务
vf导出全部数据库
网络安全线上家长会反馈
查询数据库权限oracle
后端是不是指数据库啊
计算机四级网络技术教程pdf
安徽8核服务器云服务器
宝可梦大集结无法连接服务器
石家庄微信软件开发公司
sql数据库保存
广州星际网络技术公司王名校
后端和软件开发哪个好
网络和存储服务器之间的关系
欧陆风云4搜索不到服务器