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);
        }
    }
相关推荐
鱼很腾apoc3 小时前
【学习篇】第20期 超详解 C++ 多态:从语法规则到底层原理
java·c语言·开发语言·c++·学习·算法·青少年编程
lifewange3 小时前
Redis 集合(Set)运算完全指南
数据库·chrome·redis
小许同学记录成长5 小时前
三维重建技术文档
算法·无人机
小O的算法实验室6 小时前
2026年ASOC,基于多目标优化去噪双存档进化算法+路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
phltxy6 小时前
Redis 主从复制
java·数据库·redis
2601_954526757 小时前
逆向解析Temu底层动销算法:基于API高并发轮询与全域存量透视的自动化架构重构
算法·架构·自动化
Σίσυφος19007 小时前
数据标准化(拟合的时候使用非常重要)
人工智能·算法
knight_9___7 小时前
大模型project面试7
人工智能·python·算法·面试·大模型·agent
shixiaoyu6668 小时前
Redis主从原理及哨兵搭建
redis