/**
  *
  * @param userId
  * @param period 窗口大小
  * @param maxCount 最大频次限制
  * @return
*/
public boolean isActionAllowed(String userId, int period, int maxCount) {
    String key = String.format(KEY, userId);
    long nowTs = System.currentTimeMillis();
    Jedis client = jedisPool.getResource();
    Pipeline pipe = client.pipelined();
    pipe.multi();
    pipe.zadd(key, nowTs, String.format(MEMBER, userId, nowTs));
    pipe.zremrangeByScore(key, 0, nowTs - period * 1000);
    Response<Long> count = pipe.zcard(key);
    pipe.expire(key, period + 1);
    pipe.exec();
    pipe.close();
    client.close();
    return count.get() <= maxCount;
}

 

相关文章:

  • 2021-12-18
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2021-06-20
  • 2021-11-28
  • 2021-08-12
猜你喜欢
  • 2022-12-23
  • 2021-12-25
  • 2021-11-27
  • 2021-11-10
  • 2022-12-23
  • 2021-04-17
相关资源
相似解决方案