当前位置:   article > 正文

Redis过期监听机制,订单超时自动取消_redis的监听机制

redis的监听机制

Redis过期监听机制 (Windows)

一、功能介绍

1. redis过期监听:当数据设置了过期时间,redis会根据某些机制去时时监听过期的数据
2. 应用场景: 商城中未支付过期的订单、通知(当然也可以用redis的延迟)等等
3.本篇文章只限于Windows,Linux配置差不多,只是操作系统不同

二、redis过期监听的配置

  1. 在redis安装的目录下找到redis.windows.conf文件
  2. 编辑redis.windows.conf找到配置文件中notify-keyspace-events " " 的值,
    修改为
    notify-keyspace-events Ex
    (如图下)
    在这里插入图片描述
  3. 关闭redis启动窗口,在redis安装的目录下找到start.bat重启redis (如图下)
    在这里插入图片描述
  4. 在SpringBoot集成使用
    1、引入redis相关依赖(如图下)
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述
2、创建配置类RedisListenerConfig

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @date 2023-02-21
 * @author LIZAN
 */
@Configuration
public class RedisListenerConfig {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 处理乱码
     * @return
     */
    @Bean
    public RedisTemplate redisTemplateInit() {

        // key序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //val实例化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

3、继承KeyExpirationEventMessageListener创建redis过期事件的监听类,实现onMessage方法接收过去redis数据

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @author LiZan
 * @version 1.0
 * @date 2023/2/21 14:09
 */
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对redis数据失效事件,进行数据处理
     * @param message 失效的key
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("过期redis数据:" + message.toString());
        try {
            String key = message.toString();
            //从失效key中筛选代表订单失效的key
            String orderWithKey = "order_";
            if (null !=  key && orderWithKey.startsWith(key)) {
                        log.info("订单号为【" + 123456 + "】超时未支付-自动修改为已取消状态");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【修改支付订单过期状态异常】:" + e.getMessage());
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

4、测试Redis过期监听,在redis安装的目录下找到redis-cli.exe 打开后执行redis语法写入五秒后过期的测试数据,SET order_no123213 123 EX 5
在这里插入图片描述
5、数据过期后,进入Redis过期监听方法,打印过期数据从而实现业务
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/925178
推荐阅读
相关标签
  

闽ICP备14008679号