|
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
|
|
|
public function __construct(CacheItemPoolInterface $cacheItemPool) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->cacheItemPool = $cacheItemPool; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $key |
|
52
|
|
|
* |
|
53
|
|
|
* @return ThrottlerItemInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getItem(string $key): ThrottlerItemInterface |
|
56
|
|
|
{ |
|
57
|
|
|
$item = $this->getThrottlerItem($key); |
|
58
|
|
|
|
|
59
|
|
|
return $item; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
public function hasItem(string $key): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->cacheItemPool->hasItem($key); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $key |
|
74
|
|
|
* @param mixed $item |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setItem(string $key, ThrottlerItemInterface $item): bool |
|
78
|
|
|
{ |
|
79
|
|
|
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
|
|
|
private function getThrottlerItem(string $key): ThrottlerItemInterface |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
|
|
$item = $this->cacheItemPool->getItem($key); |
|
101
|
|
|
|
|
102
|
|
|
if ($item->isHit()) { |
|
103
|
|
|
$params = json_decode($item->get(), true); |
|
104
|
|
|
$class = $params['class']; |
|
105
|
|
|
|
|
106
|
|
|
return $class::createFromArray($params['data']); |
|
107
|
|
|
} |
|
108
|
|
|
} catch (PsrCacheException $e) { |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
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
|
|
|
private function setThrottlerItem(string $key, ThrottlerItemInterface $item) |
|
122
|
|
|
{ |
|
123
|
|
|
try { |
|
124
|
|
|
$cacheItem = $this->cacheItemPool->getItem($key); |
|
125
|
|
|
|
|
126
|
|
|
if ($cacheItem->isHit()) { |
|
127
|
|
|
$this->cacheItemPool->deleteItem($key); |
|
128
|
|
|
} |
|
129
|
|
|
} catch (PsrCacheException $e) { |
|
130
|
|
|
throw new CacheAdapterException($e->getMessage(), $e->getCode(), $e->getPrevious()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$cacheItem->set( |
|
134
|
|
|
json_encode( |
|
135
|
|
|
[ |
|
136
|
|
|
'class' => get_class($item), |
|
137
|
|
|
'data' => $item, |
|
138
|
|
|
] |
|
139
|
|
|
) |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
$cacheItem->expiresAfter($item->getTtl()); |
|
143
|
|
|
|
|
144
|
|
|
return $this->cacheItemPool->save($cacheItem); |
|
145
|
|
|
} |
|
146
|
|
|
} |