class Predis in Redis 8
Same name in this branch
- 8 src/Lock/Predis.php \Drupal\redis\Lock\Predis
- 8 src/Flood/Predis.php \Drupal\redis\Flood\Predis
- 8 src/Queue/Predis.php \Drupal\redis\Queue\Predis
- 8 src/Client/Predis.php \Drupal\redis\Client\Predis
- 8 src/Cache/Predis.php \Drupal\redis\Cache\Predis
- 8 src/PersistentLock/Predis.php \Drupal\redis\PersistentLock\Predis
Defines the database flood backend. This is the default Drupal backend.
Hierarchy
- class \Drupal\redis\Flood\Predis implements FloodInterface uses RedisPrefixTrait
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\FloodView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Predis:: |
protected | property | ||
Predis:: |
protected | property | The request stack. | |
Predis:: |
public | function |
Makes the flood control mechanism forget an event for the current visitor. Overrides FloodInterface:: |
|
Predis:: |
public | function |
Cleans up expired flood events. This method is called automatically on
cron run. Overrides FloodInterface:: |
|
Predis:: |
public | function |
Checks whether a user is allowed to proceed with the specified event. Overrides FloodInterface:: |
|
Predis:: |
public | function |
Registers an event for the current visitor to the flood control mechanism. Overrides FloodInterface:: |
|
Predis:: |
public | function | Construct the PhpRedis flood backend. | |
RedisPrefixTrait:: |
protected | property | ||
RedisPrefixTrait:: |
protected | function | Get global default prefix | |
RedisPrefixTrait:: |
protected | function | Get prefix | |
RedisPrefixTrait:: |
public | function | Set prefix |