| @@ 9-91 (lines=83) @@ | ||
| 6 | ||
| 7 | use Noxlogic\RateLimitBundle\Service\RateLimitInfo; |
|
| 8 | ||
| 9 | class PhpRedis implements StorageInterface |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var \Redis |
|
| 13 | */ |
|
| 14 | protected $client; |
|
| 15 | ||
| 16 | public function __construct(\Redis $client) |
|
| 17 | { |
|
| 18 | $this->client = $client; |
|
| 19 | } |
|
| 20 | ||
| 21 | public function getRateInfo($key) |
|
| 22 | { |
|
| 23 | $key = $this->sanitizeRedisKey($key); |
|
| 24 | ||
| 25 | $info = $this->client->hgetall($key); |
|
| 26 | if (!isset($info['limit']) || !isset($info['calls']) || !isset($info['reset'])) { |
|
| 27 | return false; |
|
| 28 | } |
|
| 29 | ||
| 30 | $rateLimitInfo = new RateLimitInfo(); |
|
| 31 | $rateLimitInfo->setLimit($info['limit']); |
|
| 32 | $rateLimitInfo->setCalls($info['calls']); |
|
| 33 | $rateLimitInfo->setResetTimestamp($info['reset']); |
|
| 34 | ||
| 35 | return $rateLimitInfo; |
|
| 36 | } |
|
| 37 | ||
| 38 | public function limitRate($key) |
|
| 39 | { |
|
| 40 | $key = $this->sanitizeRedisKey($key); |
|
| 41 | ||
| 42 | $info = $this->getRateInfo($key); |
|
| 43 | if (!$info) { |
|
| 44 | return false; |
|
| 45 | } |
|
| 46 | ||
| 47 | $calls = $this->client->hincrby($key, 'calls', 1); |
|
| 48 | $info->setCalls($calls); |
|
| 49 | ||
| 50 | return $info; |
|
| 51 | } |
|
| 52 | ||
| 53 | public function createRate($key, $limit, $period) |
|
| 54 | { |
|
| 55 | $key = $this->sanitizeRedisKey($key); |
|
| 56 | ||
| 57 | $reset = time() + $period; |
|
| 58 | ||
| 59 | $this->client->hset($key, 'limit', $limit); |
|
| 60 | $this->client->hset($key, 'calls', 1); |
|
| 61 | $this->client->hset($key, 'reset', $reset); |
|
| 62 | $this->client->expire($key, $period); |
|
| 63 | ||
| 64 | $rateLimitInfo = new RateLimitInfo(); |
|
| 65 | $rateLimitInfo->setLimit($limit); |
|
| 66 | $rateLimitInfo->setCalls(1); |
|
| 67 | $rateLimitInfo->setResetTimestamp($reset); |
|
| 68 | ||
| 69 | return $rateLimitInfo; |
|
| 70 | } |
|
| 71 | ||
| 72 | public function resetRate($key) |
|
| 73 | { |
|
| 74 | $key = $this->sanitizeRedisKey($key); |
|
| 75 | ||
| 76 | $this->client->del($key); |
|
| 77 | ||
| 78 | return true; |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * Sanitizies key so it can be used safely in REDIS |
|
| 83 | * |
|
| 84 | * @param $key |
|
| 85 | * @return string|string[] |
|
| 86 | */ |
|
| 87 | protected function sanitizeRedisKey($key) { |
|
| 88 | return str_replace(str_split('@{}()/\:'), '_', $key); |
|
| 89 | } |
|
| 90 | ||
| 91 | } |
|
| 92 | ||
| @@ 8-90 (lines=83) @@ | ||
| 5 | use Noxlogic\RateLimitBundle\Service\RateLimitInfo; |
|
| 6 | use Predis\ClientInterface; |
|
| 7 | ||
| 8 | class Redis implements StorageInterface |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * @var \Predis\ClientInterface |
|
| 12 | */ |
|
| 13 | protected $client; |
|
| 14 | ||
| 15 | public function __construct(ClientInterface $client) |
|
| 16 | { |
|
| 17 | $this->client = $client; |
|
| 18 | } |
|
| 19 | ||
| 20 | public function getRateInfo($key) |
|
| 21 | { |
|
| 22 | $key = $this->sanitizeRedisKey($key); |
|
| 23 | ||
| 24 | $info = $this->client->hgetall($key); |
|
| 25 | if (!isset($info['limit']) || !isset($info['calls']) || !isset($info['reset'])) { |
|
| 26 | return false; |
|
| 27 | } |
|
| 28 | ||
| 29 | $rateLimitInfo = new RateLimitInfo(); |
|
| 30 | $rateLimitInfo->setLimit($info['limit']); |
|
| 31 | $rateLimitInfo->setCalls($info['calls']); |
|
| 32 | $rateLimitInfo->setResetTimestamp($info['reset']); |
|
| 33 | ||
| 34 | return $rateLimitInfo; |
|
| 35 | } |
|
| 36 | ||
| 37 | public function limitRate($key) |
|
| 38 | { |
|
| 39 | $key = $this->sanitizeRedisKey($key); |
|
| 40 | ||
| 41 | $info = $this->getRateInfo($key); |
|
| 42 | if (!$info) { |
|
| 43 | return false; |
|
| 44 | } |
|
| 45 | ||
| 46 | $calls = $this->client->hincrby($key, 'calls', 1); |
|
| 47 | $info->setCalls($calls); |
|
| 48 | ||
| 49 | return $info; |
|
| 50 | } |
|
| 51 | ||
| 52 | public function createRate($key, $limit, $period) |
|
| 53 | { |
|
| 54 | $key = $this->sanitizeRedisKey($key); |
|
| 55 | ||
| 56 | $reset = time() + $period; |
|
| 57 | ||
| 58 | $this->client->hset($key, 'limit', $limit); |
|
| 59 | $this->client->hset($key, 'calls', 1); |
|
| 60 | $this->client->hset($key, 'reset', $reset); |
|
| 61 | $this->client->expire($key, $period); |
|
| 62 | ||
| 63 | $rateLimitInfo = new RateLimitInfo(); |
|
| 64 | $rateLimitInfo->setLimit($limit); |
|
| 65 | $rateLimitInfo->setCalls(1); |
|
| 66 | $rateLimitInfo->setResetTimestamp($reset); |
|
| 67 | ||
| 68 | return $rateLimitInfo; |
|
| 69 | } |
|
| 70 | ||
| 71 | public function resetRate($key) |
|
| 72 | { |
|
| 73 | $key = $this->sanitizeRedisKey($key); |
|
| 74 | ||
| 75 | $this->client->del($key); |
|
| 76 | ||
| 77 | return true; |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * Sanitizies key so it can be used safely in REDIS |
|
| 82 | * |
|
| 83 | * @param $key |
|
| 84 | * @return string|string[] |
|
| 85 | */ |
|
| 86 | protected function sanitizeRedisKey($key) { |
|
| 87 | return str_replace(str_split('@{}()/\:'), '_', $key); |
|
| 88 | } |
|
| 89 | ||
| 90 | } |
|
| 91 | ||