|
1
|
|
|
<?php |
|
2
|
|
|
namespace Madewithlove\IlluminatePsrCacheBridge\Laravel; |
|
3
|
|
|
|
|
4
|
|
|
use DateTimeImmutable; |
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Cache\Repository; |
|
7
|
|
|
use Madewithlove\IlluminatePsrCacheBridge\Exceptions\InvalidArgumentException; |
|
8
|
|
|
use Psr\Cache\CacheItemInterface; |
|
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
10
|
|
|
|
|
11
|
|
|
class CacheItemPool implements CacheItemPoolInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Illuminate\Contracts\Cache\Repository |
|
15
|
|
|
*/ |
|
16
|
|
|
private $repository; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Psr\Cache\CacheItemInterface[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $deferred = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $repository |
|
25
|
|
|
*/ |
|
26
|
141 |
|
public function __construct(Repository $repository) |
|
27
|
|
|
{ |
|
28
|
141 |
|
$this->repository = $repository; |
|
29
|
141 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Destructor. |
|
33
|
|
|
*/ |
|
34
|
23 |
|
public function __destruct() |
|
35
|
|
|
{ |
|
36
|
23 |
|
$this->commit(); |
|
37
|
23 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
76 |
|
public function getItem($key) |
|
43
|
|
|
{ |
|
44
|
76 |
|
$this->validateKey($key); |
|
45
|
|
|
|
|
46
|
59 |
|
if (isset($this->deferred[$key])) { |
|
47
|
5 |
|
return clone($this->deferred[$key]); |
|
48
|
59 |
|
} elseif ($this->repository->has($key)) { |
|
49
|
26 |
|
return new CacheItem($key, unserialize($this->repository->get($key)), true); |
|
50
|
|
|
} else { |
|
51
|
57 |
|
return new CacheItem($key); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getItems(array $keys = array()) |
|
59
|
|
|
{ |
|
60
|
22 |
|
return array_combine($keys, array_map(function ($key) { |
|
61
|
20 |
|
return $this->getItem($key); |
|
62
|
22 |
|
}, $keys)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
34 |
|
public function hasItem($key) |
|
69
|
|
|
{ |
|
70
|
34 |
|
$this->validateKey($key); |
|
71
|
|
|
|
|
72
|
16 |
|
if (isset($this->deferred[$key])) { |
|
73
|
2 |
|
$item = $this->deferred[$key]; |
|
74
|
2 |
|
$expiresAt = $this->getExpiresAt($item); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
2 |
|
if (! $expiresAt) { |
|
77
|
1 |
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return $expiresAt > new DateTimeImmutable(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
14 |
|
return $this->repository->has($key); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
121 |
|
public function clear() |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
121 |
|
$this->deferred = []; |
|
93
|
121 |
|
$store = $this->repository; |
|
94
|
|
|
/* @var \Illuminate\Contracts\Cache\Store $store */ |
|
95
|
121 |
|
$store->flush(); |
|
96
|
1 |
|
} catch (Exception $exception) { |
|
97
|
1 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
120 |
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
26 |
|
public function deleteItem($key) |
|
107
|
|
|
{ |
|
108
|
26 |
|
$this->validateKey($key); |
|
109
|
|
|
|
|
110
|
8 |
|
unset($this->deferred[$key]); |
|
111
|
|
|
|
|
112
|
8 |
|
if (! $this->hasItem($key)) { |
|
113
|
5 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
5 |
|
return $this->repository->forget($key); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
22 |
|
public function deleteItems(array $keys) |
|
123
|
|
|
{ |
|
124
|
|
|
// Validating all keys first. |
|
125
|
22 |
|
foreach ($keys as $key) { |
|
126
|
21 |
|
$this->validateKey($key); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
4 |
|
$success = true; |
|
130
|
|
|
|
|
131
|
4 |
|
foreach ($keys as $key) { |
|
132
|
3 |
|
$success = $success && $this->deleteItem($key); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
4 |
|
return $success; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
35 |
|
public function save(CacheItemInterface $item) |
|
142
|
|
|
{ |
|
143
|
35 |
|
$expiresAt = $this->getExpiresAt($item); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
35 |
|
if (! $expiresAt) { |
|
146
|
|
|
try { |
|
147
|
30 |
|
$this->repository->forever($item->getKey(), serialize($item->get())); |
|
148
|
1 |
|
} catch (Exception $exception) { |
|
149
|
1 |
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
29 |
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
5 |
|
$now = new DateTimeImmutable('now', $expiresAt->getTimezone()); |
|
156
|
|
|
|
|
157
|
5 |
|
$seconds = $expiresAt->getTimestamp() - $now->getTimestamp(); |
|
158
|
5 |
|
$minutes = (int) floor($seconds / 60.0); |
|
159
|
|
|
|
|
160
|
5 |
|
if ($minutes <= 0) { |
|
161
|
3 |
|
$this->repository->forget($item->getKey()); |
|
162
|
|
|
|
|
163
|
3 |
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
try { |
|
167
|
2 |
|
$this->repository->put($item->getKey(), serialize($item->get()), $minutes); |
|
168
|
|
|
} catch (Exception $exception) { |
|
169
|
|
|
return false; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
2 |
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
12 |
|
public function saveDeferred(CacheItemInterface $item) |
|
179
|
|
|
{ |
|
180
|
12 |
|
$expiresAt = $this->getExpiresAt($item); |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
12 |
|
if ($expiresAt && ($expiresAt < new DateTimeImmutable())) { |
|
183
|
1 |
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
11 |
|
$item = (new CacheItem($item->getKey(), $item->get(), true))->expiresAt($expiresAt); |
|
187
|
|
|
|
|
188
|
11 |
|
$this->deferred[$item->getKey()] = $item; |
|
189
|
|
|
|
|
190
|
11 |
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritdoc} |
|
195
|
|
|
*/ |
|
196
|
31 |
|
public function commit() |
|
197
|
|
|
{ |
|
198
|
31 |
|
$success = true; |
|
199
|
|
|
|
|
200
|
31 |
|
foreach ($this->deferred as $key => $item) { |
|
201
|
9 |
|
$success = $success && $this->save($item); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
31 |
|
$this->deferred = []; |
|
205
|
|
|
|
|
206
|
31 |
|
return $success; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string $key |
|
211
|
|
|
* |
|
212
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
213
|
|
|
*/ |
|
214
|
135 |
|
private function validateKey($key) |
|
215
|
|
|
{ |
|
216
|
135 |
|
if (!is_string($key) || preg_match('#[{}\(\)/\\\\@:]#', $key)) { |
|
217
|
88 |
|
throw new InvalidArgumentException(); |
|
218
|
|
|
} |
|
219
|
82 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param \Madewithlove\IlluminatePsrCacheBridge\Laravel\CacheItem $item |
|
223
|
|
|
* |
|
224
|
|
|
* @return \DateTimeInterface |
|
225
|
|
|
*/ |
|
226
|
38 |
|
private function getExpiresAt(CacheItem $item) |
|
227
|
|
|
{ |
|
228
|
38 |
|
return $item->getExpiresAt(); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.