You are here

class PhpRedis in Redis 8

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

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

Hierarchy

Expanded class hierarchy of PhpRedis

1 file declares its use of PhpRedis
RedisFloodTest.php in tests/src/Kernel/RedisFloodTest.php
4 string references to 'PhpRedis'
ClientFactory::getClientInterface in src/ClientFactory.php
Lazy instantiates client proxy depending on the actual configuration.
PhpRedis::getName in src/Client/PhpRedis.php
Get underlying library name used.
RedisCacheTagsChecksum::doInvalidateTags in src/Cache/RedisCacheTagsChecksum.php
Marks cache items with any of the specified tags as invalid.
RedisTestInterfaceTrait::getRedisInterfaceEnv in tests/src/Traits/RedisTestInterfaceTrait.php
Uses an env variable to set the redis client to use for this test.

File

src/Flood/PhpRedis.php, line 13

Namespace

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

  /**
   * @var \Redis
   */
  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
PhpRedis::$client protected property
PhpRedis::$requestStack protected property The request stack.
PhpRedis::clear public function Makes the flood control mechanism forget an event for the current visitor. Overrides FloodInterface::clear
PhpRedis::garbageCollection public function Cleans up expired flood events. This method is called automatically on cron run. Overrides FloodInterface::garbageCollection
PhpRedis::isAllowed public function Checks whether a user is allowed to proceed with the specified event. Overrides FloodInterface::isAllowed
PhpRedis::register public function Registers an event for the current visitor to the flood control mechanism. Overrides FloodInterface::register
PhpRedis::__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