当前位置: 代码网 > it编程>数据库>Redis > Redis过期监听机制,订单超时自动取消方式

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

2024年06月17日 Redis 我要评论
redis过期监听机制 (windows)一、功能介绍1. redis过期监听:当数据设置了过期时间,redis会根据某些机制去时时监听过期的数据2. 应用场景: 商城中未支付过期的订单、通知(当然也

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>

  • 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;
    }
}
  • 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());
        }
    }
}
  • 4、测试redis过期监听,在redis安装的目录下找到redis-cli.exe 打开后执行redis语法写入五秒后过期的测试数据,set order_no123213 123 ex 5

  • 5、数据过期后,进入redis过期监听方法,打印过期数据从而实现业务

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com