zoukankan      html  css  js  c++  java
  • redis 简单限流

    /**
     * @Description: 简单限流
     * @Author : myron
     * @Date : 2020-05-09 17:43
     **/
    public class SimpleLimiter {
        private Jedis jedis;
        public SimpleLimiter(Jedis jedis) {
            this.jedis = jedis;
        }
        public boolean isActionAllowed(String userId, String actionKey, int period, int maxCount) {
            String key = String.format("hist:%s:%s", userId, actionKey);
            long nowTs = System.currentTimeMillis();
            Pipeline pipe = jedis.pipelined();
            pipe.multi();
            pipe.zadd(key, nowTs, "" + nowTs);
            pipe.zremrangeByScore(key, 0, nowTs - period * 1000);
            Response<Long> count = pipe.zcard(key);
            pipe.expire(key, period + 1);
            pipe.exec();
            try {
                pipe.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return count.get() <= maxCount;
        }
        public static void main(String[] args) {
            Jedis jedis = new Jedis();
            SimpleLimiter limiter = new SimpleLimiter(jedis);
            for(int i=0;i<20;i++) {
                //一分钟之内最多回复5次
                System.out.println(limiter.isActionAllowed("myron", "reply", 60, 5));
            }
        }
    }
  • 相关阅读:
    图匹配板子
    线性基
    Berlekamp-Massey algorithm
    组合/概率/形式幂级数/多项式/集合幂级数的题
    费用流 Dijkstra 原始对偶方法(primal-dual method)
    UVA-12304
    二项式系数
    卡特兰数
    多项式模板
    工具
  • 原文地址:https://www.cnblogs.com/mmh760/p/12859144.html
Copyright © 2011-2022 走看看