*/ private $counters = array(); /** * Increment a hashed key and return the new count. * * @param string $hashed_key Already-hashed key. * @param int $window_seconds Window length. * @param int $timestamp Current timestamp. */ public function increment( string $hashed_key, int $window_seconds, int $timestamp ): int { $window_seconds = max( 1, $window_seconds ); if ( ! isset( $this->counters[ $hashed_key ] ) || $this->counters[ $hashed_key ]['expires'] <= $timestamp ) { $this->counters[ $hashed_key ] = array( 'count' => 0, 'expires' => $timestamp + $window_seconds, ); } ++$this->counters[ $hashed_key ]['count']; return $this->counters[ $hashed_key ]['count']; } }