(三)延时任务篇——通过redis的zset数据结构,实现延迟任务实战

前言

在前一篇内容中我们介绍了如何使用redis key过期失效的监控,完成任务延时关闭的功能,同时官方并不支持使用此种方式实现,由于其安全性较低,存在数据丢失的情况。本节内容是对延迟任务的又一实现方案,通过redis zset的数据结构完成延迟任务。使用一个常量order作为key,订单id作为value值,任务的过期时间作为score值,通过定时任务取前100名的数据,比较score值和当前时间的大小,如果该值小于当前时间,则证明该订单已过期,完成关单的操作,并将该订单数据在redis中删除。

正文

  • 项目集成redis,可参考上一节内容
  • 引入redis的pom依赖

    org.apache.commons commons-pool2 org.springframework.boot spring-boot-starter-data-redis-reactive
  • 在application.yml中添加redis的配置信息

    spring:
    data:
    redis:
    host: 127.0.0.1
    port: 6379
    database: 0
    connect-timeout: 30000
    timeout: 30000
    lettuce:
    pool:
    enabled: true
    max-active: 200
    max-idle: 50
    max-wait: -1
    min-idle: 10
    shutdown-timeout: 100

  • 创建订单测试接口
java 复制代码
    @Operation(summary = "创建订单-redis-zset")
    @PostMapping("saveOrderByRedisZet")
    public ApiResponse saveOrderByRedisZet() {
        String orderId = String.valueOf(IdWorker.getId());
        stringRedisTemplate.opsForZSet().add("order", orderId, System.currentTimeMillis() + 60000);
        return ApiResponse.ok();
    }
  • 开启定时任务,监控订单的状态
java 复制代码
package com.yundi.xyxc.tps.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;


@Slf4j
@EnableScheduling
@Component
public class OrderJob {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 注意:如果是分布式架构,这里需要使用分布式任务的方式,例如xxl-job或者使用分布式锁+定时任务
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void createOrder() {
        ZSetOperations<String, String> operations = stringRedisTemplate.opsForZSet();
        //查询过期排名前100的订单
        Set<ZSetOperations.TypedTuple<String>> top100 = operations.rangeWithScores("order", 0, 99);
        assert top100 != null;
        for (ZSetOperations.TypedTuple<String> tuple : top100) {
            log.info("Member: {}, Score: {}", tuple.getValue(), tuple.getScore());
            //订单小于当前时间,订单过期,删除该订单
            if (tuple.getScore() < System.currentTimeMillis()) {
                log.info("订单过期,删除订单:{}", tuple.getValue());
                operations.remove("order", tuple.getValue());
            }
        }
        log.info("--------------------------------------------------------------");

    }

}

**PS:**此处需要注意的是,如果是分布式系统,此处需要使用分布锁+@Scheduled定时任务,保证数据的安全性,以免产生并发问题。同时需要保证定时任务的执行时间合理根据任务的实际完成时间。

  • 启动项目,通过接口调用订单创建接口
  • 查看关单输出,过期的订单被关单并删除

结语

使用redis的zset数据结构完成延迟任务,相较于监控key失效更加安全,不会出现数据的丢失,同时相较于某些mq消息中间件的延迟对列,更加灵活,可以设置任意时间的延迟过期任务。关于使用redis的zset数据结构,实现延迟任务的内容到这里就结束了。。。

相关推荐
笃行3504 小时前
KingbaseES KSQL Developer 的连接管理、数据处理与对象管理
数据库
ha_lydms4 小时前
MaxCompute中加密与解密函数
大数据·数据库·数据仓库·hadoop·dataworks·maxcompute·odps
Shadow(⊙o⊙)4 小时前
MySQL内置函数
数据库·mysql
笃行3505 小时前
金仓 KingbaseES PL/SQL 开发全攻略:异常处理、数据类型、调试优化与过程语句
数据库
快乐的木子李6 小时前
mysql-26.7.0-linux26.7.0下载安装
数据库·mysql
excel6 小时前
prisma 如何处理数据库竞态
前端·数据库·后端
知行产研7 小时前
易控智驾:毛利率跃升17.2个百分点,矿山无人驾驶从“规模换增长“迈入“盈利拐点“
数据库·mysql
千里码aicood7 小时前
基于CART算法的图书分类系统设计与实现
数据库·算法·分类
饺子大魔王的男人7 小时前
Redis 内存和连接异常怎么排查?用 redis_exporter 搭一套可远程抓取的监控链路
数据库·redis·bootstrap
可涵不会debug7 小时前
第一次学 LangGraph:用一个快递案例搞懂 State、Node、Edge 和 compile
服务器·数据库·mysql