1 | <?php |
||
11 | final class MemcachedRateLimiter implements RateLimiter, SilentRateLimiter |
||
12 | { |
||
13 | private const MEMCACHED_SECONDS_LIMIT = 2592000; // Number of seconds in 30 days |
||
14 | |||
15 | /** @var Memcached */ |
||
16 | private $memcached; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $keyPrefix; |
||
20 | |||
21 | 5 | public function __construct(Memcached $memcached, string $keyPrefix = '') |
|
31 | |||
32 | 2 | public function limit(string $identifier, Rate $rate): void |
|
43 | |||
44 | 3 | public function limitSilently(string $identifier, Rate $rate): Status |
|
62 | |||
63 | 5 | private function limitKey(string $identifier, int $interval): string |
|
67 | |||
68 | 3 | private function timeKey(string $identifier, int $interval): string |
|
72 | |||
73 | 5 | private function getCurrent(string $limitKey): int |
|
77 | |||
78 | 3 | private function updateCounterAndTime(string $limitKey, string $timeKey, int $interval): int |
|
88 | |||
89 | 5 | private function updateCounter(string $limitKey, int $interval): int |
|
95 | |||
96 | 3 | private function getElapsedTime(string $timeKey): int |
|
100 | |||
101 | /** |
||
102 | * Interval to Memcached expiration time. |
||
103 | * |
||
104 | * @see https://www.php.net/manual/en/memcached.expiration.php |
||
105 | * |
||
106 | * @param int $interval |
||
107 | * |
||
108 | * @return int |
||
109 | */ |
||
110 | 5 | private function intervalToMemcachedTime(int $interval): int |
|
114 | } |
||
115 |