|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunspikes\src\Throttle\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemInterface; |
|
6
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
7
|
|
|
use Sunspikes\src\Throttle\Cache\Bridge\CacheItemBridge; |
|
8
|
|
|
|
|
9
|
|
|
class ThrottlerCache implements ThrottlerCacheInterface |
|
10
|
|
|
{ |
|
11
|
|
|
const CACHE_ITEM_EXPIRY = 0; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var CacheItemPoolInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $cacheItemPool; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ThrottlerCache constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(CacheItemPoolInterface $cacheItemPool) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->cacheItemPool = $cacheItemPool; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $key |
|
30
|
|
|
* @param string $count |
|
31
|
|
|
* @param string $limit |
|
32
|
|
|
* @param string $ttl |
|
33
|
|
|
*/ |
|
34
|
|
|
public function set(string $key, string $count, string $limit, string $ttl = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$params = json_encode([ |
|
37
|
|
|
'count' => $count, |
|
38
|
|
|
'limit' => $limit, |
|
39
|
|
|
'ttl' => $ttl ?? microtime(true), |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
$this->setParams($key, $params); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $key |
|
47
|
|
|
* |
|
48
|
|
|
* @return int|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public function count(string $key) |
|
51
|
|
|
{ |
|
52
|
|
|
$params = $this->getParams($key); |
|
53
|
|
|
$count = $params ? (int) $params['count'] : null; |
|
54
|
|
|
|
|
55
|
|
|
return $count; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $key |
|
60
|
|
|
*/ |
|
61
|
|
|
public function increment(string $key) |
|
62
|
|
|
{ |
|
63
|
|
|
$params = $this->getParams($key); |
|
64
|
|
|
$params['count']++; |
|
65
|
|
|
|
|
66
|
|
|
$this->setParams($key, $params); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $key |
|
71
|
|
|
*/ |
|
72
|
|
|
public function remove(string $key) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->cacheItemPool->deleteItem($key); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $key |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function hasItem(string $key): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->cacheItemPool->hasItem($key); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $key |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function isExpired(string $key): bool |
|
93
|
|
|
{ |
|
94
|
|
|
$params = $this->getParams($key); |
|
95
|
|
|
|
|
96
|
|
|
return microtime(true) < $params['ttl']; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $key |
|
101
|
|
|
* |
|
102
|
|
|
* @return array|null |
|
103
|
|
|
*/ |
|
104
|
|
|
private function getParams(string $key) |
|
105
|
|
|
{ |
|
106
|
|
|
$item = $this->cacheItemPool->getItem($key); |
|
107
|
|
|
$params = $item ? json_decode($item->get()) : null; |
|
108
|
|
|
|
|
109
|
|
|
return $params; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* @param array $params |
|
115
|
|
|
* |
|
116
|
|
|
* @return CacheItemInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
private function setParams(string $key, array $params): CacheItemInterface |
|
119
|
|
|
{ |
|
120
|
|
|
$item = $this->cacheItemPool->getItem($key) ?? new CacheItemBridge($key); |
|
121
|
|
|
$item->set(json_encode($params)); |
|
122
|
|
|
$item->expiresAt(self::CACHE_ITEM_EXPIRY); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$this->cacheItemPool->deleteItem($key); |
|
125
|
|
|
$this->cacheItemPool->save($item); |
|
126
|
|
|
|
|
127
|
|
|
return $item; |
|
128
|
|
|
} |
|
129
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: