redistemplate hash 过期时间_Redis过期监听——订单超时-取消
最近在做电商项目,涉及支付超时处理的几种方式。【记录哈使用redis监听处理】
提交订单的时候,支付-超过了有效时间则支付状态自动更新为已取消。
欢迎交流
redis过期监听的实现:
1.修改redis.windows.conf配置文件中notify-keyspace-events的值
默认配置notify-keyspace-events的值为 ""
修改为 notify-keyspace-events Ex 这样便开启了过期事件
2. 创建配置类RedisListenerConfig(配置RedisMessageListenerContainer这个Bean)
package com.zjt.shop.config;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;/*** 作者:zhuLin* 日期:2020-08-21 11:48* 备注:redis监听配置*/@Configuration
public class RedisListenerConfig {@Autowiredprivate RedisTemplate redisTemplate;/*** 处理乱码* @return*/@Beanpublic RedisTemplate redisTemplateInit() {// key序列化redisTemplate.setKeySerializer(new StringRedisSerializer());//val实例化redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());return redisTemplate;}@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);return container;}}
3.继承KeyExpirationEventMessageListener创建redis过期事件的监听类
package com.zjt.shop.common.util;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zjt.shop.modules.order.service.OrderInfoService;
import com.zjt.shop.modules.product.entity.OrderInfoEntity;
import com.zjt.shop.modules.product.mapper.OrderInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;/*** 作者:zhuLin* 日期:2020-08-21 11:51* 备注:redis数据失效事件*/
@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {super(listenerContainer);}@Autowiredprivate OrderInfoMapper orderInfoMapper;/*** 针对redis数据失效事件,进行数据处理* @param message* @param pattern*/@Overridepublic void onMessage(Message message, byte[] pattern) {try {String key = message.toString();//从失效key中筛选代表订单失效的keyif (key != null && key.startsWith("order_")) {//截取订单号,查询订单,如果是未支付状态则为-取消订单String orderNo = key.substring(6);QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();queryWrapper.eq("order_no",orderNo);OrderInfoEntity orderInfo = orderInfoMapper.selectOne(queryWrapper);if (orderInfo != null) {if (orderInfo.getOrderState() == 0) { //待支付orderInfo.setOrderState(4); //已取消orderInfoMapper.updateById(orderInfo);log.info("订单号为【" + orderNo + "】超时未支付-自动修改为已取消状态");}}}} catch (Exception e) {e.printStackTrace();log.error("【修改支付订单过期状态异常】:" + e.getMessage());}}
}
4:测试
通过redis客户端存一个有效时间为3s的订单:
结果:
redistemplate hash 过期时间_Redis过期监听——订单超时-取消相关推荐
- c# redis 如何设置过期时间_Redis 过期时间与内存管理
http://www.redis.cn/commands/expire.html http://www.redis.cn/topics/lru-cache.html 内存管理 当 Redis 作为缓存 ...
- redis map 过期时间_redis 过期时间
设置key的过期时间,超过时间后,将会自动删除该key.在Redis的术语中一个key的相关超时是不确定的. 超时后只有对key执行DEL命令或者SET命令或者GETSET时才会清除. 这意味着,从概 ...
- picker 监听确定和取消事件
picker 监听确定和取消事件 <picker mode="multiSelector" @change="confirmHandling" @canc ...
- c# redis 如何设置过期时间_Redis Hash存储token、及设置过期时间
相信大家在很多业务场景都会遇到对数据设置过期时间,如安全认证token.短信验证码等.一般我们的解决有几种:存redis.存数据库.实时计算(如JWT),不管使用哪种方式,需求就是过期了对数据的清理. ...
- redis hash field过期时间_Redis系列-Redis数据类型
1.全局命令 1.1查看所有键 127.0.0.1:6379> set k1 111OK127.0.0.1:6379> set k2 222OK127.0.0.1:6379> set ...
- redistemplate.opsforhash设置过期时间_Redis详解(十一)------ 过期删除策略和内存淘汰策略...
大家好,我是可乐,一个专注原创,乐于分享的程序猿. 本系列教程持续更新,可以微信搜索「 IT可乐 」第一时间阅读.回复<电子书>有我为大家特别筛选的海量免费书籍资料 在介绍这篇文章之前,我 ...
- redis 缓存过期默认时间_Redis 过期时间与内存管理
http://www.redis.cn/commands/expire.html http://www.redis.cn/topics/lru-cache.html 内存管理 当 Redis 作为缓存 ...
- c# redis hashid如何设置过期时间_Redis中Key过期策略amp;淘汰机制
1. Redis中设置Key过期时间 我们有两种方式设置过期时间 1.1 设置多久后过期 设置一个 key 10s 过期,可以这样 127.0.0.1:6379> SET key value E ...
- java redis设置过期时间_Redis的一些核心原理
点关注,不迷路:持续更新Java相关技术及资讯!!! 一.Redis的单线程和高性能 Redis 单线程为什么还能这么快? 因为它所有的数据都在内存中,所有的运算都是内存级别的运算(纳秒),而且单线程 ...
最新文章
- 交流电流变换器 : SCT1013 特性测试
- 遍历同辈节电的方法_jQuery遍历
- 现代制造工程笔记05-表面工程技术
- 程序员太牛了 公司年会整成代码讨论会
- nmap配合shell使用
- Excel 宏工作簿 VBAProject 工程保护 - 代码不可查看
- hj212协议如何和php通讯,5G/4G边缘计算网关如何实现HJ212协议上报
- java实现千米与经纬度度数的转换(画圆左右有精度缺失)
- 爬取豆瓣电影储存到数据库MONGDB中以及反反爬虫
- Mac没有winnt格式_Mac视频格式转换工具-H265 Converter Pro
- HG5520A型多用表校准仪
- python爬虫——40行代码爬取「笔趣看」全部小说
- 避免选中页面文字或者内容时出现蓝色背景
- 怎么创建邮箱帐号?教育邮箱
- MongoDB全面总结
- 诺基亚heif源码 cmake
- Outlook如何将导航栏从左侧移动到底部
- BBEdit 10.X for mac的lincese
- idea,eclipse和jdk环境配置
- 企业微信外部群机器人api接口sdk登陆
热门文章
- Lucene和Solr版本对应关系
- ubuntu下Pure-FTPd的安装和配置
- 用freebsd搭建日志服务器
- 二叉树遍历(深度优先+广度优先)
- Azure PowerShell (10) 使用PowerShell导出订阅下所有的Azure VM和Cloud Service的高可用情况...
- ccf——201903-4 消息传递接口
- 「学习记录」《数值分析》第二章计算实习题(Python语言)
- killall pkill kill
- Spring Boot工程在IDEA中运行报错
- 5 shell命令之tr