Predis.php in Redis 8
File
src/Flood/Predis.php
View source
<?php
namespace Drupal\redis\Flood;
use Drupal\Core\Flood\FloodInterface;
use Drupal\redis\ClientFactory;
use Drupal\redis\RedisPrefixTrait;
use Symfony\Component\HttpFoundation\RequestStack;
class Predis implements FloodInterface {
use RedisPrefixTrait;
protected $client;
protected $requestStack;
public function __construct(ClientFactory $client_factory, RequestStack $request_stack) {
$this->client = $client_factory
->getClient();
$this->requestStack = $request_stack;
}
public function register($name, $window = 3600, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
$key = $this
->getPrefix() . ':flood:' . $name . ':' . $identifier;
$this->client
->zAdd($key, $_SERVER['REQUEST_TIME'] + $window, microtime(TRUE));
$this->client
->expire($key, $_SERVER['REQUEST_TIME'] + $window);
}
public function clear($name, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
$key = $this
->getPrefix() . ':flood:' . $name . ':' . $identifier;
$this->client
->del($key);
}
public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
$key = $this
->getPrefix() . ':flood:' . $name . ':' . $identifier;
$number = $this->client
->zCount($key, $_SERVER['REQUEST_TIME'], 'inf');
return $number < $threshold;
}
public function garbageCollection() {
}
}
Classes
Name |
Description |
Predis |
Defines the database flood backend. This is the default Drupal backend. |