$now) { $wait = $cooldownUntil - $now; throw new Exception(sprintf('检测过于频繁,请 %d 秒后再试', $wait)); } $timestamps = Cache::get($attemptsKey); if (!is_array($timestamps)) { $timestamps = []; } $timestamps = array_values(array_filter($timestamps, static function ($ts) use ($now): bool { return is_int($ts) && ($now - $ts) < self::WINDOW_SECONDS; })); if (count($timestamps) >= self::MAX_ATTEMPTS) { Cache::set($cooldownKey, $now + self::COOLDOWN_SECONDS, self::COOLDOWN_SECONDS); Cache::set($attemptsKey, $timestamps, self::WINDOW_SECONDS + self::COOLDOWN_SECONDS); throw new Exception(sprintf( '一分钟最多检测 %d 次,请 %d 秒后再试', self::MAX_ATTEMPTS, self::COOLDOWN_SECONDS )); } $timestamps[] = $now; Cache::set($attemptsKey, $timestamps, self::WINDOW_SECONDS + self::COOLDOWN_SECONDS); } }