redis实现订单自动过期功能的源码分享

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;}@Beanpublic 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@Transactionalpublic 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;@BeanRedisMessageListenerContainer 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数据库教程

以上就是redis实现订单自动过期功能的源码分享的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2042076.html

(0)
上一篇 2025年2月24日 00:34:22
下一篇 2025年2月24日 00:34:38

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 如何利用Redis高效获取和缓存产品排行榜详细信息?

    Redis缓存方案:高效获取产品排行榜详情 高效利用Redis缓存产品排行榜数据至关重要。假设已有一个包含前20个产品ID的Redis有序集合(zset),本文将探讨如何进一步优化缓存方案,高效获取和缓存产品详细信息。 首先,排行榜前20个…

    2025年4月5日
    100
  • JSON序列化与JDK序列化在存储和效率上的差异有哪些?

    JSON序列化与JDK序列化:存储与效率差异分析 Redis应用中,开发者常青睐JSON序列化,认为其效率更高、存储空间更小。然而,实践中并非总是如此,例如Pig框架的OAuth2实现就选择了JDK序列化存储token。本文将深入探讨JSO…

    2025年4月5日
    300
  • JSON序列化与JDK序列化在存储上的差异是什么?

    JSON序列化与JDK序列化:存储空间差异分析 本文探讨JSON序列化和JDK序列化两种常用方法在数据存储方面的差异。在Redis等存储系统中,选择合适的序列化方式至关重要,因为它直接影响存储效率和空间占用。 背景: 笔者在学习Redis过…

    2025年4月5日
    200
  • 如何优化Debian JS日志查询速度

    提升 Debian 系统 JavaScript (JS) 应用日志查询速度,需要多方面协同优化。以下策略能有效改善查询效率: 一、选择高效日志管理工具 rsyslog 或 syslog-ng: 这些系统日志服务比轻量级工具更高效,支持日志轮…

    2025年4月5日
    200
  • Java框架性能优化常见问题解答

    Java 框架性能优化常见问题解答 引言 在高并发和数据吞吐量高的系统中,Java 框架的性能优化至关重要。本文探讨了一些常见的性能优化问题及其对应的解决方案。 1. 数据库连接管理 立即学习“Java免费学习笔记(深入)”; 问题:应用程…

    2025年4月2日
    100
  • 利用分布式缓存优化Java框架的性能

    分布式缓存通过存储常用数据,可有效提升 java 应用程序性能。使用 redis 作为缓存,添加 redis 客户端库后即可应用。实战案例中,通过缓存用户信息,大幅提升获取速度,因为多数情况下可直接从缓存中获取数据,减少数据库查询次数。 利…

    2025年4月2日
    100
  • java延时队列怎么做

    Java延时队列是一种允许延迟插入元素的队列。实现方法包括:延迟任务调度器(适用于较短延迟)优先级队列和定时器(适用于较长延迟)Redis ZSet(优先级队列的实现)Apache ActiveMQ Delayed Delivery(适用于…

    2025年4月2日
    100
  • 哪些开源替代品具有独特的特性和优势?

    postgresql、mongodb、redis 和 mariadb 等开源数据库引擎提供独特的特性和优势:postgresql:可扩展性、安全性、jsonb 支持mongodb:文档结构、分布式架构、云服务redis:内存数据库、键值存储…

    2025年4月2日
    100
  • java分布式锁怎么用

    在分布式系统中使用分布式锁可以保证共享资源在同一时间只有一个客户端访问,以维护数据一致性和完整性。Java中常见的实现方式包括ZooKeeper、Redis和etcd。使用分布式锁的一般步骤包括获取锁、执行操作和释放锁。需要注意死锁、性能和…

    2025年4月2日
    200
  • 开源替代品是否有任何潜在的风险或限制?

    采用开源替代品时应注意潜在风险和局限性:安全性风险:开源软件的公开源代码可能为恶意行为者提供利用漏洞的机会。支持有限:开源软件通常由社区提供支持,问题解决和新功能请求可能需要较长时间。功能限制:开源软件可能缺少专有软件中某些功能或特性。性能…

    2025年4月2日
    100

发表回复

登录后才能评论