You are here

public function PhpRedis::register in Redis 8

Registers an event for the current visitor to the flood control mechanism.

Parameters

string $name: The name of an event. To prevent unintended name clashes, it is recommended to use the module name first in the event name, optionally followed by a dot and the actual event name (e.g. "mymodule.my_event").

int $window: (optional) Number of seconds before this event expires. Defaults to 3600 (1 hour). Typically uses the same value as the isAllowed() $window parameter. Expired events are purged on cron run to prevent the flood table from growing indefinitely.

string $identifier: (optional) Unique identifier of the current user. Defaults to the current user's IP address).

Overrides FloodInterface::register

File

src/Flood/PhpRedis.php, line 46

Class

PhpRedis
Defines the database flood backend. This is the default Drupal backend.

Namespace

Drupal\redis\Flood

Code

public function register($name, $window = 3600, $identifier = NULL) {
  if (!isset($identifier)) {
    $identifier = $this->requestStack
      ->getCurrentRequest()
      ->getClientIp();
  }
  $key = $this
    ->getPrefix() . ':flood:' . $name . ':' . $identifier;

  // Add a key for the event to the sorted set, the score is timestamp, so we
  // can count them easily.
  $this->client
    ->zAdd($key, $_SERVER['REQUEST_TIME'] + $window, microtime(TRUE));

  // Set or update the expiration for the sorted set, it will be removed if
  // the newest entry expired.
  $this->client
    ->expire($key, $_SERVER['REQUEST_TIME'] + $window);
}