for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace RazonYang\TokenBucket\Manager;
use RazonYang\TokenBucket\Manager;
use RazonYang\TokenBucket\SerializerInterface;
class RedisManager extends Manager
{
/**
* @var int $ttl time to live.
*/
private $ttl = 0;
* @var string $prefix key prefix.
private $prefix = '';
* @var \Redis $redis
private $redis;
public function __construct(
int $capacity,
float $rate,
SerializerInterface $serializer,
\Redis $redis,
int $ttl = 0,
string $prefix = ''
) {
parent::__construct($capacity, $rate, $serializer);
$this->redis = $redis;
$this->ttl = $ttl;
$this->prefix = $prefix;
}
protected function load(string $name)
return $this->redis->get($this->prefix . $name);
protected function save(string $name, $data)
if ($this->ttl > 0) {
$saved = $this->redis->setEx($this->prefix . $name, $this->ttl, $data);
} else {
$saved = $this->redis->set($this->prefix . $name, $data);
if (!$saved) {
throw new \RuntimeException('Unable to save allowance: ' . $this->redis->getLastError());