| 1 | <?php |
||
| 9 | class RedisAdapter implements StoreInterface |
||
| 10 | { |
||
| 11 | protected Redis $redis; |
||
|
|
|||
| 12 | |||
| 13 | |||
| 14 | public function __construct(Redis $redis) |
||
| 15 | { |
||
| 16 | $this->redis = $redis; |
||
| 17 | 6 | } |
|
| 18 | |||
| 19 | 6 | public function get(string $key): int |
|
| 20 | 6 | { |
|
| 21 | return (int)$this->redis->get($key); |
||
| 22 | 4 | } |
|
| 23 | |||
| 24 | 4 | public function inc(string $key, int $ttl = 60): int |
|
| 25 | { |
||
| 26 | $value = $this->redis->incr($key); |
||
| 27 | 5 | if ($value === 1) { |
|
| 28 | $this->redis->expire($key, $ttl); |
||
| 29 | 5 | } |
|
| 30 | 5 | ||
| 31 | 5 | return $value; |
|
| 32 | } |
||
| 33 | |||
| 34 | 5 | public function reset(string $key): bool |
|
| 35 | { |
||
| 36 | return (bool)$this->redis->del($key); |
||
| 37 | 6 | } |
|
| 38 | |||
| 39 | 6 | public function isExceeded(string $key, int $max): bool |
|
| 40 | { |
||
| 41 | $value = $this->get($key); |
||
| 42 | 1 | return $value >= $max; |
|
| 43 | } |
||
| 44 | 1 | ||
| 45 | 1 | public function ttl(string $key): ?int |
|
| 46 | { |
||
| 47 | $ttl = $this->redis->ttl($key); |
||
| 48 | 1 | return $ttl > 0 ? $ttl : null; |
|
| 49 | } |
||
| 50 | } |
||
| 51 |