/** * * @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; }