@@ 9-72 (lines=64) @@ | ||
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 | $info = $this->client->hgetall($key); |
|
24 | if (!isset($info['limit']) || !isset($info['calls']) || !isset($info['reset'])) { |
|
25 | return false; |
|
26 | } |
|
27 | ||
28 | $rateLimitInfo = new RateLimitInfo(); |
|
29 | $rateLimitInfo->setLimit($info['limit']); |
|
30 | $rateLimitInfo->setCalls($info['calls']); |
|
31 | $rateLimitInfo->setResetTimestamp($info['reset']); |
|
32 | ||
33 | return $rateLimitInfo; |
|
34 | } |
|
35 | ||
36 | public function limitRate($key) |
|
37 | { |
|
38 | $info = $this->getRateInfo($key); |
|
39 | if (!$info) { |
|
40 | return false; |
|
41 | } |
|
42 | ||
43 | $this->client->hincrby($key, 'calls', 1); |
|
44 | ||
45 | return $info; |
|
46 | } |
|
47 | ||
48 | public function createRate($key, $limit, $period) |
|
49 | { |
|
50 | $reset = time() + $period; |
|
51 | ||
52 | $this->client->hset($key, 'limit', $limit); |
|
53 | $this->client->hset($key, 'calls', 1); |
|
54 | $this->client->hset($key, 'reset', $reset); |
|
55 | $this->client->expire($key, $period); |
|
56 | ||
57 | $rateLimitInfo = new RateLimitInfo(); |
|
58 | $rateLimitInfo->setLimit($limit); |
|
59 | $rateLimitInfo->setCalls(1); |
|
60 | $rateLimitInfo->setResetTimestamp($reset); |
|
61 | ||
62 | return $rateLimitInfo; |
|
63 | } |
|
64 | ||
65 | public function resetRate($key) |
|
66 | { |
|
67 | $this->client->del($key); |
|
68 | ||
69 | return true; |
|
70 | } |
|
71 | ||
72 | } |
|
73 |
@@ 8-71 (lines=64) @@ | ||
5 | use Noxlogic\RateLimitBundle\Service\RateLimitInfo; |
|
6 | use Predis\Client; |
|
7 | ||
8 | class Redis implements StorageInterface |
|
9 | { |
|
10 | /** |
|
11 | * @var \Predis\Client |
|
12 | */ |
|
13 | protected $client; |
|
14 | ||
15 | public function __construct(Client $client) |
|
16 | { |
|
17 | $this->client = $client; |
|
18 | } |
|
19 | ||
20 | public function getRateInfo($key) |
|
21 | { |
|
22 | $info = $this->client->hgetall($key); |
|
23 | if (!isset($info['limit']) || !isset($info['calls']) || !isset($info['reset'])) { |
|
24 | return false; |
|
25 | } |
|
26 | ||
27 | $rateLimitInfo = new RateLimitInfo(); |
|
28 | $rateLimitInfo->setLimit($info['limit']); |
|
29 | $rateLimitInfo->setCalls($info['calls']); |
|
30 | $rateLimitInfo->setResetTimestamp($info['reset']); |
|
31 | ||
32 | return $rateLimitInfo; |
|
33 | } |
|
34 | ||
35 | public function limitRate($key) |
|
36 | { |
|
37 | $info = $this->getRateInfo($key); |
|
38 | if (!$info) { |
|
39 | return false; |
|
40 | } |
|
41 | ||
42 | $this->client->hincrby($key, 'calls', 1); |
|
43 | ||
44 | return $info; |
|
45 | } |
|
46 | ||
47 | public function createRate($key, $limit, $period) |
|
48 | { |
|
49 | $reset = time() + $period; |
|
50 | ||
51 | $this->client->hset($key, 'limit', $limit); |
|
52 | $this->client->hset($key, 'calls', 1); |
|
53 | $this->client->hset($key, 'reset', $reset); |
|
54 | $this->client->expire($key, $period); |
|
55 | ||
56 | $rateLimitInfo = new RateLimitInfo(); |
|
57 | $rateLimitInfo->setLimit($limit); |
|
58 | $rateLimitInfo->setCalls(1); |
|
59 | $rateLimitInfo->setResetTimestamp($reset); |
|
60 | ||
61 | return $rateLimitInfo; |
|
62 | } |
|
63 | ||
64 | public function resetRate($key) |
|
65 | { |
|
66 | $this->client->del($key); |
|
67 | ||
68 | return true; |
|
69 | } |
|
70 | ||
71 | } |
|
72 |