1
|
|
|
<?php namespace Comodojo\Cache\Providers; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Cache\Traits\StatefulTrait; |
4
|
|
|
use \Comodojo\Cache\Traits\NamespaceTrait; |
5
|
|
|
use \Comodojo\Cache\Interfaces\EnhancedCacheItemPoolInterface; |
6
|
|
|
use \Comodojo\Cache\Item; |
7
|
|
|
use \Comodojo\Cache\Components\KeyValidator; |
8
|
|
|
use \Comodojo\Foundation\Utils\UniqueId; |
9
|
|
|
use \Comodojo\Foundation\Utils\ClassProperties; |
10
|
|
|
use \Psr\Log\LoggerInterface; |
11
|
|
|
use \Psr\Cache\CacheItemInterface; |
12
|
|
|
use \DateTime; |
13
|
|
|
use \Comodojo\Exception\CacheException; |
14
|
|
|
use \Comodojo\Exception\InvalidCacheArgumentException; |
15
|
|
|
use \Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @package Comodojo Cache |
19
|
|
|
* @author Marco Giovinazzi <[email protected]> |
20
|
|
|
* @license MIT |
21
|
|
|
* |
22
|
|
|
* LICENSE: |
23
|
|
|
* |
24
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
25
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
26
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
27
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
28
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
29
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
30
|
|
|
* THE SOFTWARE. |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
abstract class AbstractEnhancedProvider |
34
|
|
|
extends AbstractProvider |
35
|
|
|
implements EnhancedCacheItemPoolInterface { |
36
|
|
|
|
37
|
|
|
use StatefulTrait; |
38
|
|
|
use NamespaceTrait; |
39
|
|
|
|
40
|
|
|
protected $driver; |
41
|
|
|
|
42
|
|
|
protected $default_properties = []; |
43
|
|
|
|
44
|
|
|
protected $properties; |
45
|
|
|
|
46
|
|
|
private $queue = []; |
47
|
|
|
|
48
|
211 |
|
public function __construct(array $properties = [], LoggerInterface $logger = null) { |
49
|
|
|
|
50
|
211 |
|
parent::__construct($logger); |
51
|
|
|
|
52
|
211 |
|
$this->properties = ClassProperties::create($this->default_properties)->merge($properties); |
53
|
|
|
|
54
|
211 |
|
$this->setId(UniqueId::generate(64)); |
55
|
|
|
|
56
|
211 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
abstract public function getStats(); |
62
|
|
|
|
63
|
82 |
|
public function getProperties() { |
64
|
|
|
|
65
|
82 |
|
return $this->properties; |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
176 |
|
public function getItem($key) { |
73
|
|
|
|
74
|
176 |
|
if ( KeyValidator::validateKey($key) === false ) { |
75
|
|
|
throw new InvalidCacheArgumentException('Invalid key provided'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
|
80
|
176 |
|
$data = $this->driver->get($key, $this->getNamespace()); |
81
|
|
|
|
82
|
|
|
} catch (Exception $e) { |
83
|
|
|
|
84
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
85
|
|
|
$data = null; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
176 |
|
if ( $data === null ) return new Item($key); |
90
|
|
|
|
91
|
93 |
|
$item = new Item($key, true); |
92
|
|
|
|
93
|
93 |
|
return $item->set(unserialize($data)); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
42 |
|
public function hasItem($key) { |
101
|
|
|
|
102
|
42 |
|
if ( KeyValidator::validateKey($key) === false ) { |
103
|
|
|
throw new InvalidCacheArgumentException('Invalid key provided'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
|
108
|
42 |
|
$data = $this->driver->has($key, $this->getNamespace()); |
109
|
|
|
|
110
|
|
|
} catch (Exception $e) { |
111
|
|
|
|
112
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
113
|
|
|
$data = false; |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
42 |
|
return $data; |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
57 |
|
public function clear() { |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
|
128
|
57 |
|
$data = $this->driver->clear(); |
129
|
|
|
|
130
|
|
|
} catch (Exception $e) { |
131
|
|
|
|
132
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
133
|
|
|
$data = false; |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
57 |
|
return $data; |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
8 |
|
public function clearNamespace() { |
145
|
|
|
|
146
|
|
|
try { |
147
|
|
|
|
148
|
8 |
|
$data = $this->driver->clear($this->getNamespace()); |
149
|
|
|
|
150
|
|
|
} catch (Exception $e) { |
151
|
|
|
|
152
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
153
|
|
|
$data = false; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
8 |
|
return $data; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
10 |
|
public function deleteItem($key) { |
165
|
|
|
|
166
|
10 |
|
if ( KeyValidator::validateKey($key) === false ) { |
167
|
|
|
throw new InvalidCacheArgumentException('Invalid key provided'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
try { |
171
|
|
|
|
172
|
10 |
|
$data = $this->driver->delete($key, $this->getNamespace()); |
173
|
|
|
|
174
|
|
|
} catch (Exception $e) { |
175
|
|
|
|
176
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
177
|
|
|
$data = false; |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
10 |
|
return $data; |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
169 |
|
public function save(CacheItemInterface $item) { |
189
|
|
|
|
190
|
169 |
|
$ttl = $item->getTtl(); |
|
|
|
|
191
|
|
|
|
192
|
169 |
|
if ( $ttl < 0 ) return false; |
193
|
|
|
|
194
|
|
|
try { |
195
|
|
|
|
196
|
120 |
|
$data = $this->driver->set($item->getKey(), $this->getNamespace(), serialize($item->getRaw()), $ttl); |
|
|
|
|
197
|
|
|
|
198
|
|
|
} catch (Exception $e) { |
199
|
|
|
|
200
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
201
|
|
|
$data = false; |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
120 |
|
return $data; |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
211 |
|
public function test() { |
213
|
|
|
|
214
|
211 |
|
if ( $this->driver->test() ) { |
215
|
|
|
|
216
|
211 |
|
$this->setState(self::CACHE_SUCCESS); |
217
|
|
|
|
218
|
211 |
|
return true; |
219
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$error = $this->driver->getName()." driver unavailable, disabling provider ".$this->getId()." administratively"; |
223
|
|
|
|
224
|
|
|
$this->logger->error($error); |
225
|
|
|
|
226
|
|
|
$this->setState(self::CACHE_ERROR, $error); |
227
|
|
|
|
228
|
|
|
return false; |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
12 |
|
public function getItems(array $keys = []) { |
236
|
|
|
|
237
|
12 |
|
if ( empty($keys) ) return []; |
238
|
|
|
|
239
|
6 |
|
$result = []; |
240
|
|
|
|
241
|
|
|
try { |
242
|
|
|
|
243
|
6 |
|
$data = $this->driver->getMultiple($keys, $this->getNamespace()); |
244
|
|
|
|
245
|
|
|
} catch (Exception $e) { |
246
|
|
|
|
247
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
248
|
|
|
$data = array_combine($keys, array_fill(0, count($keys), null)); |
249
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
6 |
|
foreach ( $data as $key => $value ) { |
253
|
|
|
|
254
|
6 |
|
if ( $value == null ) { |
255
|
|
|
$result[$key] = new Item($key); |
256
|
|
|
} else { |
257
|
6 |
|
$result[$key] = new Item($key, true); |
258
|
6 |
|
$result[$key]->set(unserialize($value)); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
6 |
|
return $result; |
264
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
12 |
|
public function deleteItems(array $keys) { |
271
|
|
|
|
272
|
|
|
try { |
273
|
|
|
|
274
|
12 |
|
$data = $this->driver->deleteMultiple($keys, $this->getNamespace()); |
275
|
|
|
|
276
|
|
|
} catch (Exception $e) { |
277
|
|
|
|
278
|
|
|
$this->setState(self::CACHE_ERROR, $e->getMessage()); |
279
|
|
|
$data = false; |
280
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
12 |
|
return $data; |
284
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* {@inheritdoc} |
289
|
|
|
*/ |
290
|
12 |
|
public function saveDeferred(CacheItemInterface $item) { |
291
|
|
|
|
292
|
12 |
|
$this->checkQueueNamespace(true); |
293
|
|
|
|
294
|
12 |
|
$namespace = $this->getNamespace(); |
295
|
|
|
|
296
|
12 |
|
$this->queue[$namespace][$item->getKey()] = $item; |
297
|
|
|
|
298
|
12 |
|
return true; |
299
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* {@inheritdoc} |
304
|
|
|
*/ |
305
|
12 |
|
public function commit() { |
306
|
|
|
|
307
|
12 |
|
$result = []; |
308
|
|
|
|
309
|
12 |
|
$active_namespace = $this->getNamespace(); |
310
|
|
|
|
311
|
12 |
|
foreach ( $this->queue as $namespace => $queue ) { |
312
|
|
|
|
313
|
12 |
|
$this->setNamespace($namespace); |
314
|
|
|
|
315
|
12 |
|
foreach ( $queue as $key => $item ) { |
316
|
|
|
|
317
|
12 |
|
$result[] = $this->save($item); |
318
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
12 |
|
$this->queue = []; |
324
|
|
|
|
325
|
12 |
|
$this->setNamespace($active_namespace); |
326
|
|
|
|
327
|
12 |
|
return in_array(false, $result) ? false : true; |
328
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
12 |
|
private function checkQueueNamespace($create = false) { |
332
|
|
|
|
333
|
12 |
|
$namespace = $this->getNamespace(); |
334
|
|
|
|
335
|
12 |
|
if ( array_key_exists($namespace, $this->queue) ) { |
336
|
6 |
|
return true; |
337
|
12 |
|
} else if ( $create ) { |
338
|
12 |
|
$this->queue[$namespace] = []; |
339
|
12 |
|
return true; |
340
|
|
|
} else { |
341
|
|
|
return false; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
} |
347
|
|
|
|