redisson滑动时间窗应用场景

概述

复制代码
前10分钟内累计3次验证失败后,增加图形验证码验证条件,前10分钟内累计6次验证失败后,系统自动锁定该账号15分钟,15分钟后自动解锁;

方案

基于redisson(zset)滑动时间窗记录最近10分钟内该账户登录失败次数

  • 统计次数、每次失败加1
java 复制代码
  /**
     * 统计请求次数
     *
     * @param windowTime 窗口时间,单位:秒
     * @param key        登录账户
     * @return 请求次数
     */
    public Integer count(int windowTime, String key) {
        // 窗口结束时间
        long windowEndTime = System.currentTimeMillis();
        // 窗口开始时间
        long windowStartTime = windowEndTime - windowTime * 1000L;

        try {
            // 创建 RBatch 实例,批量执行命令
            RBatch batch = redissonClient.createBatch();

            // 添加元素 score=当前时间戳 value=请求序列号,唯一不可重复
            batch.getScoredSortedSet(key).addAsync(windowEndTime, UUID.randomUUID().toString());

            // 统计数据
            batch.getScoredSortedSet(key).countAsync(windowStartTime, true, windowEndTime, true);
            // 清除窗口过期成员
            batch.getScoredSortedSet(key).removeRangeByScoreAsync(0, true, windowStartTime, false);
            // 设置key过期时间
            batch.getScoredSortedSet(key).expireAsync(Duration.ofSeconds(windowTime));

            // 执行管道命令
            BatchResult<?> batchResult = batch.execute();


            // 返回统计数量
            List<?> responses = batchResult.getResponses();
            Integer number = (Integer) responses.get(1);

            return number;
        } catch (Exception e) {
            log.error("统计请求次数异常!", e);
            return null;
        }
    }
  • 获取登录失败次数
java 复制代码
/**
     * 获取对应窗口的次数
     * @param windowTime 窗口大小十分钟,600s
     * @param key
     * @return
     */
    public Integer getLastCachedCount(int windowTime, String key) {
        // 窗口结束时间
        long windowEndTime = System.currentTimeMillis();
        // 窗口开始时间
        long windowStartTime = windowEndTime - windowTime * 1000L;

        try {
            // 创建 RBatch 实例,批量执行命令
            RBatch batch = redissonClient.createBatch();

            // 统计数据,获取上一次缓存存取的数据
            RFuture<Integer> countFuture = batch.getScoredSortedSet(key).countAsync(windowStartTime, true, windowEndTime, true);
            // 执行管道命令
            batch.execute();

            // 等待统计数据完成并获取结果
            Integer count = countFuture.get();
// 如果结果为null,则返回0,否则返回计数值
            return count == null ? 0 : count.intValue();
        } catch (Exception e) {
            log.error("获取上一次缓存存取数据异常!", e);
            // 如果出现异常,返回null
            return null;
        }
    }
  • 清除登录失败次数
java 复制代码
    public void clearCachedCount(int windowTime, String key) {
        // 窗口结束时间
        long windowEndTime = System.currentTimeMillis();
        // 窗口开始时间
        long windowStartTime = windowEndTime - windowTime * 1000L;

        try {
            // 创建 RBatch 实例,批量执行命令
            RBatch batch = redissonClient.createBatch();

            // 统计数据,获取上一次缓存存取的数据
            batch.getScoredSortedSet(key).removeRangeByScoreAsync(windowStartTime, true, windowEndTime, true);
            // 执行管道命令
            batch.execute();
// 如果结果为null,则返回0,否则返回计数值
        } catch (Exception e) {
            log.error("清楚登录失败记录次数异常", e);
        }
    }
相关推荐
吐泡泡_3 分钟前
Redis(主从复制)
redis
惯导马工1 小时前
【论文导读】IDOL: Inertial Deep Orientation-Estimation and Localization
深度学习·算法
老姜洛克1 小时前
自然语言处理(NLP)之n-gram从原理到实战
算法·nlp
CoovallyAIHub2 小时前
基于YOLO集成模型的无人机多光谱风电部件缺陷检测
深度学习·算法·计算机视觉
CoovallyAIHub2 小时前
几十个像素的小目标,为何难倒无人机?LCW-YOLO让无人机小目标检测不再卡顿
深度学习·算法·计算机视觉
怀旧,2 小时前
【C++】19. 封装红⿊树实现set和map
linux·c++·算法
凯子坚持 c2 小时前
Redis核心通用命令深度解析:结合C++ redis-plus-plus 实战指南
c++·redis·log4j
往事随风去2 小时前
Redis的内存淘汰策略(Eviction Policies)有哪些?
redis·后端·算法
神里流~霜灭2 小时前
(C++)数据结构初阶(顺序表的实现)
linux·c语言·数据结构·c++·算法·顺序表·单链表