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

相关文章

  • redis过期回调功能实现示例

    redis过期回调功能实现示例

    Redis提供了一种过期回调的机制,可以在某个键过期时触发一个回调函数,本文就来介绍一下redis过期回调功能实现示例,感兴趣的可以了解一下
    2023-09-09
  • Redis学习教程之命令的执行过程详解

    Redis学习教程之命令的执行过程详解

    这篇文章主要给大家介绍了关于Redis学习教程之命令的执行过程的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • Redisson分布式锁解锁异常问题

    Redisson分布式锁解锁异常问题

    文章主要描述了在使用Redisson进行分布式锁操作时,遇到的`IllegalMonitorStateException`异常,通过分析报错信息,作者发现了问题的根源在于线程尝试解锁但未按顺序执行,作者提出了两种解决方案:在解锁前增加判断,确保线程已获得锁
    2024-12-12
  • Redis报错“NOAUTH Authentication required”两种解决方案

    Redis报错“NOAUTH Authentication required”两种解决方案

    Redis提供了一个命令行工具redis-cli,它允许你直接连接到Redis服务器,如果你知道Redis服务器的密码,你可以在连接时直接提供它,本文给大家介绍连接了Redis报错“NOAUTH Authentication required”两种解决方案
    2024-05-05
  • Redis如何实现分布式锁

    Redis如何实现分布式锁

    相信大家对锁已经不陌生了,本文主要介绍了Redis如何实现分布式锁,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 通过kubesphere部署redis的方法

    通过kubesphere部署redis的方法

    这篇文章主要介绍了通过kubesphere部署redis的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Redis 命令的详解及简单实例

    Redis 命令的详解及简单实例

    这篇文章主要介绍了Redis 命令的详解及简单实例的相关资料,这里提供基础语法及使用实例,需要的朋友可以参考下
    2017-08-08
  • 基于redis分布式锁实现秒杀功能

    基于redis分布式锁实现秒杀功能

    这篇文章主要为大家详细介绍了基于redis分布式锁实现秒杀功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Redis中缓存预热与缓存穿透解决方案

    Redis中缓存预热与缓存穿透解决方案

    Redis缓存预热与缓存穿透是Redis缓存使用中的两个重要概念,文章首先介绍了Redis缓存预热和缓存穿透的基本概念,然后详细阐述了它们的产生原因和解决方案,感兴趣的可以了解一下
    2023-12-12
  • Redis的数据过期清除策略实现

    Redis的数据过期清除策略实现

    Redis实现了数据过期清除策略,本文将深入解析Redis的数据过期清除策略,包括过期键的删除方式、清除策略的选择以及相关配置参数的介绍,感兴趣的可以了解一下
    2024-05-05

最新评论