@@ 9-66 (lines=58) @@ | ||
6 | use Noxlogic\RateLimitBundle\Service\RateLimitInfo; |
|
7 | use Noxlogic\RateLimitBundle\Entity\PdoHandler; |
|
8 | ||
9 | class Database implements StorageInterface |
|
10 | { |
|
11 | //define client |
|
12 | /** |
|
13 | * @var PdoHandler |
|
14 | */ |
|
15 | protected $client; |
|
16 | ||
17 | public function __construct($client) { |
|
18 | $this->client = $client; |
|
19 | } |
|
20 | ||
21 | public function getRateInfo($key) { |
|
22 | $info = $this->client->fetch($key); |
|
23 | ||
24 | $rateLimitInfo = new RateLimitInfo(); |
|
25 | $rateLimitInfo->setLimit($info['limit']); |
|
26 | $rateLimitInfo->setCalls($info['calls']); |
|
27 | $rateLimitInfo->setResetTimestamp($info['reset']); |
|
28 | ||
29 | return $rateLimitInfo; |
|
30 | } |
|
31 | ||
32 | public function limitRate($key) { |
|
33 | $info = $this->client->fetch($key); |
|
34 | if ($info === false || !array_key_exists('limit', $info)) { |
|
35 | return false; |
|
36 | } |
|
37 | ||
38 | $info['calls']++; |
|
39 | ||
40 | $expire = $info['reset'] - time(); |
|
41 | ||
42 | $this->client->save($key, $info, $expire); |
|
43 | ||
44 | return $this->getRateInfo($key); |
|
45 | } |
|
46 | ||
47 | public function createRate($key, $limit, $period) { |
|
48 | $info = array(); |
|
49 | $info['limit'] = $limit; |
|
50 | $info['calls'] = 1; |
|
51 | $info['reset'] = time() + $period; |
|
52 | ||
53 | $this->client->save($key, $info); |
|
54 | ||
55 | return $this->getRateInfo($key); |
|
56 | } |
|
57 | ||
58 | public function resetRate($key) { |
|
59 | $this->client->delete($key); |
|
60 | return true; |
|
61 | } |
|
62 | ||
63 | public function fetch($key){ |
|
64 | return $this->client->fetch($key); |
|
65 | } |
|
66 | } |
@@ 8-57 (lines=50) @@ | ||
5 | use Doctrine\Common\Cache\Cache; |
|
6 | use Noxlogic\RateLimitBundle\Service\RateLimitInfo; |
|
7 | ||
8 | class DoctrineCache implements StorageInterface { |
|
9 | /** @var \Doctrine\Common\Cache\Cache */ |
|
10 | protected $client; |
|
11 | ||
12 | public function __construct(Cache $client) { |
|
13 | $this->client = $client; |
|
14 | } |
|
15 | ||
16 | public function getRateInfo($key) { |
|
17 | $info = $this->client->fetch($key); |
|
18 | ||
19 | $rateLimitInfo = new RateLimitInfo(); |
|
20 | $rateLimitInfo->setLimit($info['limit']); |
|
21 | $rateLimitInfo->setCalls($info['calls']); |
|
22 | $rateLimitInfo->setResetTimestamp($info['reset']); |
|
23 | ||
24 | return $rateLimitInfo; |
|
25 | } |
|
26 | ||
27 | public function limitRate($key) { |
|
28 | $info = $this->client->fetch($key); |
|
29 | if ($info === false || !array_key_exists('limit', $info)) { |
|
30 | return false; |
|
31 | } |
|
32 | ||
33 | $info['calls']++; |
|
34 | ||
35 | $expire = $info['reset'] - time(); |
|
36 | ||
37 | $this->client->save($key, $info, $expire); |
|
38 | ||
39 | return $this->getRateInfo($key); |
|
40 | } |
|
41 | ||
42 | public function createRate($key, $limit, $period) { |
|
43 | $info = array(); |
|
44 | $info['limit'] = $limit; |
|
45 | $info['calls'] = 1; |
|
46 | $info['reset'] = time() + $period; |
|
47 | ||
48 | $this->client->save($key, $info, $period); |
|
49 | ||
50 | return $this->getRateInfo($key); |
|
51 | } |
|
52 | ||
53 | public function resetRate($key) { |
|
54 | $this->client->delete($key); |
|
55 | return true; |
|
56 | } |
|
57 | } |
|
58 |