惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
T
Tor Project blog
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
MongoDB | Blog
MongoDB | Blog
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
小众软件
小众软件
G
GRAHAM CLULEY
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
人人都是产品经理
人人都是产品经理
S
Schneier on Security
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
E
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
The Register - Security
The Register - Security
S
Securelist
Martin Fowler
Martin Fowler
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog

博客园 - 技术宅home

kali捕获wifi握手包 centos7安装mysql8 【继续教育】选准用好“三支一扶”人员,助力乡村振兴战略 【继续教育】深入学***在长三角一体化发展座谈会的重要讲话精神,推动长三角区域一体化和高质量发展 【继续教育】全球战“疫”斗争中的中国优势、 中国力量、中国担当、中国精神 【继续教育】民法典总则编解读 【继续教育】坚持创新在我国现代化建设全局中的核心地位,把科技自立自强作为国家发展的战略支撑 【继续教育】加快构建完整内需体系,形成国内国际双循环相互促进新格局 查看当前服务器中最占内存的程序 百度智能云之语音技术(自动播报语音) maven多环境IDEA启动报Do not use @ for indentation错误解决方法 StringRedisTemplate获取redis信息 MySQL规范 微信测试公众号手动发送模板消息 spring-boot-starter-parent 与 spring-boot-dependencies Docker 容器知识点 Docker 常用命令 Docker 镜像知识点 Centos离线下载Aria2 AriaNG bt-tracker自动更新
redis key过期提醒,实现订单自动取消
技术宅home · 2020-01-21 · via 博客园 - 技术宅home

需求

  处理订单下单后30分钟未付款自动取消

解决方案

  利用redis中key自动过期机制,提交订单时将订单编号写入redis,并设置30分钟的过期时间,当订单过期后,取到过期的key然后做业务处理。

功能开发

  1、开启redis过期提醒

    本次使用lunix版本redis。修改redis.conf 中1061行中【notify-keyspace-events ""】修改为【notify-keyspace-events Ex】

    相关配置参数说明

K:keyspace事件,事件以__keyspace@<db>__为前缀进行发布;         
E:keyevent事件,事件以__keyevent@<db>__为前缀进行发布;         
g:一般性的,非特定类型的命令,比如del,expire,rename等;        
$:字符串特定命令;         
l:列表特定命令;         
s:集合特定命令;         
h:哈希特定命令;         
z:有序集合特定命令;         
x:过期事件,当某个键过期并删除时会产生该事件;         
e:驱逐事件,当某个键因maxmemore策略而被删除时,产生该事件;         
A:g$lshzxe的别名,因此”AKE”意味着所有事件。

  2、重启redis服务

  3、代码中设置redis配置

@Configuration
public class RedisListenerConfig {
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}
/**
 * 监听所有db的过期事件__keyevent@*__:expired"
 */
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    @Autowired
    ShOrderService shOrderService;
    /**
     * 针对redis数据失效事件,进行数据处理
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
        String expiredKey = message.toString();
        log.info("======================redis time out========================");
        if(expiredKey.startsWith(ShSysConstant.ORDER_PENDING)){
            // 取到业务数据进行处理
            String orderNumber = expiredKey.substring(6);
            log.info("======================"+orderNumber+"======================");
            shOrderService.cancelOrderByRedis(orderNumber);
        }
    }
}