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 IP地址的绑定的实现
May 08 Redis
聊一聊Redis与MySQL双写一致性如何保证
Jun 26 Redis
redis使用不当导致应用卡死bug的过程解析
Jul 01 Redis
redis客户端实现高可用读写分离的方式详解
Jul 04 Redis
Redis 彻底禁用RDB持久化操作
Jul 09 Redis
Redis中缓存穿透/击穿/雪崩问题和解决方法
Dec 04 Redis
解决linux下redis数据库overcommit_memory问题
Feb 24 Redis
muduo TcpServer模块源码分析
Apr 26 Redis
Redis keys命令的具体使用
Jun 05 Redis
浅谈Redis缓冲区机制
Jun 05 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 团购折扣计算公式
2011/11/24 PHP
PHP间隔一段时间执行代码的方法
2014/12/02 PHP
php使用memcoder将视频转成mp4格式的方法
2015/03/12 PHP
详解PHP的Yii框架中的Controller控制器
2016/03/29 PHP
理解Javascript_06_理解对象的创建过程
2010/10/15 Javascript
基于jQuery实现的Ajax 验证用户名是否存在的实现代码
2011/04/06 Javascript
Nodejs实现多人同时在线移动鼠标的小游戏分享
2014/12/06 NodeJs
JavaScript比较当前时间是否在指定时间段内的方法
2016/08/02 Javascript
jQuery实现点击关注和取消功能
2017/07/03 jQuery
原生JS实现Ajax跨域请求flask响应内容
2017/10/24 Javascript
js构造函数创建对象是否加new问题
2018/01/22 Javascript
Vue多种方法实现表头和首列固定的示例代码
2018/02/02 Javascript
Node.js + express实现上传大文件的方法分析【图片、文本文件】
2019/03/14 Javascript
基于JS正则表达式实现模板数据动态渲染(实现思路详解)
2020/03/07 Javascript
Python Mysql数据库操作 Perl操作Mysql数据库
2009/01/12 Python
Linux下使用python调用top命令获得CPU利用率
2015/03/10 Python
Python制作钉钉加密/解密工具
2016/12/07 Python
python 3.7.0 下pillow安装方法
2018/08/27 Python
如何更优雅地写python代码
2019/07/02 Python
基于Python中isfile函数和isdir函数使用详解
2019/11/29 Python
pytorch实现特殊的Module--Sqeuential三种写法
2020/01/15 Python
pandas DataFrame运算的实现
2020/06/14 Python
详解python3 GUI刷屏器(附源码)
2021/02/18 Python
CSS3中的Media Queries学习笔记
2016/05/23 HTML / CSS
css3实现书本翻页效果的示例代码
2021/03/08 HTML / CSS
泰国王权免税店官方网站:KingPower
2019/03/11 全球购物
adidas马来西亚官网:adidas MY
2020/09/12 全球购物
中学运动会广播稿
2014/01/19 职场文书
展会邀请函范文
2014/01/26 职场文书
会计专业大学生求职信范文
2014/01/28 职场文书
文案策划求职信
2014/03/18 职场文书
环境监测与治理技术专业求职信
2014/07/06 职场文书
关于读书的演讲稿600字
2014/08/27 职场文书
农村老人去世追悼词
2015/06/23 职场文书
mysql多表查询-笔记七
2021/04/05 MySQL
SQL写法--行行比较
2021/08/23 SQL Server