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

 更新时间:2021年05月08日 11:51:40   作者:SvenJoe  
这篇文章主要介绍了Redis实现订单自动过期功能的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

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

配置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 订单自动过期内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Windows系统安装Redis的详细图文教程

    Windows系统安装Redis的详细图文教程

    但有时候想在windows下折腾下Redis,那么就可以参考下面的方法了,虽然脚本之家小编以前整理了一些,发现这篇做的比较详细,下载也给出来了
    2018-08-08
  • Redis数据库的应用场景介绍

    Redis数据库的应用场景介绍

    这篇文章主要介绍了Redis数据库的应用场景介绍,本文讲解了MySql+Memcached架构的问题、Redis常用数据类型、Redis数据类型应用和实现方式、Redis实际应用场景等内容,需要的朋友可以参考下
    2015-06-06
  • redis中redisson实现锁自动延时

    redis中redisson实现锁自动延时

    redisson作为分布式锁能够解决分布式的加锁解锁问题,还能够实现锁的设置存活时间以及自动续期,本文主要介绍了redis中redisson实现锁自动延时,感兴趣的可以了解一下
    2024-02-02
  • Redis的过期键删除策略原理说明

    Redis的过期键删除策略原理说明

    这篇文章主要介绍了Redis的过期键删除策略原理说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 一篇文章带你弄清楚Redis的精髓

    一篇文章带你弄清楚Redis的精髓

    Redis是一个开源的、支持网络、基于内存的键值对存储系统,它可以用作数据库、缓存和消息中间件。它支持多种数据类型,包括字符串、散列、列表、集合、位图等,拥有极快的读写速度,并且支持丰富的特性,如事务、持久化、复制、脚本、发布/订阅等。
    2023-02-02
  • redis通过位图法记录在线用户的状态详解

    redis通过位图法记录在线用户的状态详解

    这篇文章主要给大家介绍了关于redis如何通过位图法记录在线用户的状态的相关资料,文中先对位图进行了一个简单的介绍,而后通过示例代码将实现的方法介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • Linux、Windows下Redis的安装即Redis的基本使用详解

    Linux、Windows下Redis的安装即Redis的基本使用详解

    Redis是一个基于内存的key-value结构数据库,Redis 是互联网技术领域使用最为广泛的存储中间件,这篇文章主要介绍了Linux、Windows下Redis的安装即Redis的基本使用详解,需要的朋友可以参考下
    2022-09-09
  • 提高redis缓存命中率的方法

    提高redis缓存命中率的方法

    在本篇文章里小编给大家整理了关于怎么提高redis缓存命中率的相关知识点内容,有兴趣的朋友们跟着学习下。
    2019-06-06
  • 分割超大Redis数据库例子

    分割超大Redis数据库例子

    这篇文章主要介绍了分割超大Redis数据库例子,本文讲解了分割的需求、分割的思路及分割实例,需要的朋友可以参考下
    2015-03-03
  • Redis总结笔记(一):安装和常用命令

    Redis总结笔记(一):安装和常用命令

    这篇文章主要介绍了Redis总结笔记(一):安装和常用命令,本文着重总结了常用命令,如对value操作的命令、对String操作的命令、对List操作的命令、对Set操作的命令等,需要的朋友可以参考下
    2015-01-01

最新评论