Redis实现订单自动过期功能的示例代码

用户下单后,规定XX分钟后自动设置为“已过期”,不能再发起支付。项目类似此类"过期"的需求,笔者提供一种使用Redis的解决思路,结合Redis的订阅、发布和键空间通知机制(Keyspace Notifications)进行实现。

Posted in Redis onMay 08, 2021

配置redis.confg

notify-keyspace-events选项默认是不启用,改为notify-keyspace-events “Ex”。重启生效,索引位i的库,每当有过期的元素被删除时,向**keyspace@:expired**频道发送通知。
E表示键事件通知,所有通知以__keyevent@__:expired为前缀;
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 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实现订单自动过期功能的示例代码的文章就介绍到这了,更多相关Redis 订单自动过期内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Redis 相关文章推荐
浅谈Redis存储数据类型及存取值方法
May 08 Redis
基于Redis实现分布式锁的方法(lua脚本版)
May 12 Redis
Java Socket实现Redis客户端的详细说明
May 26 Redis
浅谈Redis的几个过期策略
May 27 Redis
详解Redis基本命令与使用场景
Jun 01 Redis
浅谈Redis位图(Bitmap)及Redis二进制中的问题
Jul 15 Redis
在项目中使用redis做缓存的一些思路
Sep 14 Redis
Redis 持久化 RDB 与 AOF的执行过程
Nov 07 Redis
Redis之RedisTemplate配置方式(序列和反序列化)
Mar 13 Redis
分布式架构Redis中有哪些数据结构及底层实现原理
Mar 13 Redis
Redis实现一个账号只能登录一个设备
Apr 19 Redis
redis 限制内存使用大小的实现
使用Redis实现秒杀功能的简单方法
Redis6.0搭建集群Redis-cluster的方法
May 08 #Redis
浅谈Redis存储数据类型及存取值方法
Redis IP地址的绑定的实现
May 08 #Redis
redis通过6379端口无法连接服务器(redis-server.exe闪退)
redis 查看所有的key方式
You might like
让PHP更快的提供文件下载的代码
2012/06/13 PHP
php判断并删除空目录及空子目录的方法
2015/02/11 PHP
php通过array_merge()函数合并关联和非关联数组的方法
2015/03/18 PHP
PHP批量修改文件名称的方法分析
2017/02/27 PHP
如何利用预加载优化Laravel Model查询详解
2017/08/11 PHP
JS中setInterval、setTimeout不能传递带参数的函数的解决方案
2013/04/28 Javascript
YUI模块开发原理详解
2013/11/18 Javascript
JQuery以JSON方式提交数据到服务端示例代码
2014/05/05 Javascript
jQuery中:only-child选择器用法实例
2015/01/03 Javascript
JavaScript获取浏览器信息的方法
2015/11/20 Javascript
javascript动画系列之模拟滚动条
2016/12/13 Javascript
简单实现JS计算器功能
2016/12/21 Javascript
Router解决跨模块下的页面跳转示例
2018/01/11 Javascript
Vue中如何实现proxy代理
2018/04/20 Javascript
js实现京东秒杀倒计时功能
2019/01/21 Javascript
Vuex的actions属性的具体使用
2019/04/14 Javascript
原生js实现日期选择插件
2020/05/21 Javascript
使用python删除nginx缓存文件示例(python文件操作)
2014/03/26 Python
Python os模块介绍
2014/11/30 Python
关于Python中浮点数精度处理的技巧总结
2017/08/10 Python
ubuntu系统下使用pm2设置nodejs开机自启动的方法
2018/05/12 NodeJs
Python绘制KS曲线的实现方法
2018/08/13 Python
Python Django框架单元测试之文件上传测试示例
2019/05/17 Python
Python 3.6 中使用pdfminer解析pdf文件的实现
2019/09/25 Python
html5 初试 indexedDB(推荐)
2016/07/21 HTML / CSS
欧洲最大的笔和书写专家:The Pen Shop
2017/03/19 全球购物
阿迪达斯德国官方网站:adidas德国
2017/07/12 全球购物
农业资源与环境专业自荐信范文
2013/12/30 职场文书
2014学年自我鉴定
2014/02/23 职场文书
遗嘱公证书标准样本
2014/04/08 职场文书
2014年客房部工作总结
2014/11/22 职场文书
2015年房产销售工作总结范文
2015/05/22 职场文书
2016大学生求职自荐信范文
2016/01/28 职场文书
《打电话》教学反思
2016/02/22 职场文书
党组织关系的介绍信模板
2019/06/21 职场文书
Vue深入理解插槽slot的使用
2022/08/05 Vue.js