You are here

class Predis in Redis 8

Same name in this branch
  1. 8 src/Lock/Predis.php \Drupal\redis\Lock\Predis
  2. 8 src/Flood/Predis.php \Drupal\redis\Flood\Predis
  3. 8 src/Queue/Predis.php \Drupal\redis\Queue\Predis
  4. 8 src/Client/Predis.php \Drupal\redis\Client\Predis
  5. 8 src/Cache/Predis.php \Drupal\redis\Cache\Predis
  6. 8 src/PersistentLock/Predis.php \Drupal\redis\PersistentLock\Predis

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

Hierarchy

Expanded class hierarchy of Predis

3 string references to 'Predis'
ClientFactory::getClientInterface in src/ClientFactory.php
Lazy instantiates client proxy depending on the actual configuration.
Predis::getName in src/Client/Predis.php
Get underlying library name used.
RedisCacheTagsChecksum::doInvalidateTags in src/Cache/RedisCacheTagsChecksum.php
Marks cache items with any of the specified tags as invalid.

File

src/Flood/Predis.php, line 13

Namespace

Drupal\redis\Flood
View source
class Predis implements FloodInterface {
  use RedisPrefixTrait;

  /**
   * @var \Predis\Client
   */
  protected $client;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Construct the PhpRedis flood backend.
   *
   * @param \Drupal\redis\ClientFactory $client_factory
   *   The database connection which will be used to store the flood event
   *   information.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack used to retrieve the current request.
   */
  public function __construct(ClientFactory $client_factory, RequestStack $request_stack) {
    $this->client = $client_factory
      ->getClient();
    $this->requestStack = $request_stack;
  }

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

  /**
   * {@inheritdoc}
   */
  public function clear($name, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->requestStack
        ->getCurrentRequest()
        ->getClientIp();
    }
    $key = $this
      ->getPrefix() . ':flood:' . $name . ':' . $identifier;
    $this->client
      ->del($key);
  }

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

    // Count the in the last $window seconds.
    $number = $this->client
      ->zCount($key, $_SERVER['REQUEST_TIME'], 'inf');
    return $number < $threshold;
  }

  /**
   * {@inheritdoc}
   */
  public function garbageCollection() {

    // No garbage collection necessary.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Predis::$client protected property
Predis::$requestStack protected property The request stack.
Predis::clear public function Makes the flood control mechanism forget an event for the current visitor. Overrides FloodInterface::clear
Predis::garbageCollection public function Cleans up expired flood events. This method is called automatically on cron run. Overrides FloodInterface::garbageCollection
Predis::isAllowed public function Checks whether a user is allowed to proceed with the specified event. Overrides FloodInterface::isAllowed
Predis::register public function Registers an event for the current visitor to the flood control mechanism. Overrides FloodInterface::register
Predis::__construct public function Construct the PhpRedis flood backend.
RedisPrefixTrait::$prefix protected property
RedisPrefixTrait::getDefaultPrefix protected function Get global default prefix
RedisPrefixTrait::getPrefix protected function Get prefix
RedisPrefixTrait::setPrefix public function Set prefix