1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 Krishnaprasad MG <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Sunspikes\Ratelimit\Cache; |
27
|
|
|
|
28
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
29
|
|
|
use Psr\Cache\CacheException as PsrCacheException; |
30
|
|
|
use Sunspikes\Ratelimit\Cache\Exception\CacheAdapterException; |
31
|
|
|
use Sunspikes\Ratelimit\Cache\Exception\ItemNotFoundException; |
32
|
|
|
|
33
|
|
|
class ThrottlerCache implements ThrottlerCacheInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var CacheItemPoolInterface |
37
|
|
|
*/ |
38
|
|
|
private $cacheItemPool; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ThrottlerCache constructor. |
42
|
|
|
* |
43
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
44
|
|
|
*/ |
45
|
31 |
|
public function __construct(CacheItemPoolInterface $cacheItemPool) |
46
|
|
|
{ |
47
|
31 |
|
$this->cacheItemPool = $cacheItemPool; |
48
|
31 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* |
53
|
|
|
* @return ThrottlerItemInterface |
54
|
|
|
*/ |
55
|
29 |
|
public function getItem(string $key): ThrottlerItemInterface |
56
|
|
|
{ |
57
|
29 |
|
$item = $this->getThrottlerItem($key); |
58
|
|
|
|
59
|
18 |
|
return $item; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $key |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
1 |
|
public function hasItem(string $key): bool |
68
|
|
|
{ |
69
|
1 |
|
return $this->cacheItemPool->hasItem($key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $key |
74
|
|
|
* @param mixed $item |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
28 |
|
public function setItem(string $key, ThrottlerItemInterface $item): bool |
78
|
|
|
{ |
79
|
28 |
|
return $this->setThrottlerItem($key, $item); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $key |
84
|
|
|
*/ |
85
|
|
|
public function removeItem(string $key) |
86
|
|
|
{ |
87
|
|
|
$this->cacheItemPool->deleteItem($key); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $key |
92
|
|
|
* |
93
|
|
|
* @return ThrottlerItemInterface |
94
|
|
|
* @throws CacheAdapterException |
95
|
|
|
* @throws ItemNotFoundException |
96
|
|
|
*/ |
97
|
29 |
|
private function getThrottlerItem(string $key): ThrottlerItemInterface |
98
|
|
|
{ |
99
|
|
|
try { |
100
|
29 |
|
$item = $this->cacheItemPool->getItem($key); |
101
|
|
|
|
102
|
28 |
|
if ($item->isHit()) { |
103
|
18 |
|
$params = json_decode($item->get(), true); |
104
|
18 |
|
$class = $params['class']; |
105
|
|
|
|
106
|
28 |
|
return $class::createFromArray($params['data']); |
107
|
|
|
} |
108
|
1 |
|
} catch (PsrCacheException $e) { |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
28 |
|
throw new ItemNotFoundException('Item not found.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $key |
116
|
|
|
* @param ThrottlerItemInterface $item |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
* @throws CacheAdapterException |
120
|
|
|
*/ |
121
|
28 |
|
private function setThrottlerItem(string $key, ThrottlerItemInterface $item) |
122
|
|
|
{ |
123
|
|
|
try { |
124
|
28 |
|
$cacheItem = $this->cacheItemPool->getItem($key); |
125
|
|
|
|
126
|
28 |
|
if ($cacheItem->isHit()) { |
127
|
28 |
|
$this->cacheItemPool->deleteItem($key); |
128
|
|
|
} |
129
|
|
|
} catch (PsrCacheException $e) { |
130
|
|
|
throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious()); |
131
|
|
|
} |
132
|
|
|
|
133
|
28 |
|
$cacheItem->set( |
134
|
28 |
|
json_encode( |
135
|
|
|
[ |
136
|
28 |
|
'class' => get_class($item), |
137
|
28 |
|
'data' => $item, |
138
|
|
|
] |
139
|
|
|
) |
140
|
|
|
); |
141
|
|
|
|
142
|
28 |
|
$cacheItem->expiresAfter($item->getTtl()); |
143
|
|
|
|
144
|
28 |
|
return $this->cacheItemPool->save($cacheItem); |
145
|
|
|
} |
146
|
|
|
} |