1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Core\Pool; |
16
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use phpFastCache\Core\Item\ExtendedCacheItemInterface; |
19
|
|
|
use phpFastCache\EventManager; |
20
|
|
|
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; |
21
|
|
|
use Psr\Cache\CacheItemInterface; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
trait ExtendedCacheItemPoolTrait |
25
|
|
|
{ |
26
|
|
|
use CacheItemPoolTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function clean() |
32
|
|
|
{ |
33
|
|
|
trigger_error('Cache clean() method is deprecated, use clear() method instead', E_USER_DEPRECATED); |
34
|
|
|
return $this->clear(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512) |
41
|
|
|
{ |
42
|
|
|
$callback = function (CacheItemInterface $item) { |
43
|
|
|
return $item->get(); |
44
|
|
|
}; |
45
|
|
|
return json_encode(array_map($callback, array_values($this->getItems($keys))), $option, $depth); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function getItemsByTag($tagName) |
52
|
|
|
{ |
53
|
|
|
if (is_string($tagName)) { |
54
|
|
|
$driverResponse = $this->getItem($this->getTagKey($tagName)); |
55
|
|
|
if ($driverResponse->isHit()) { |
56
|
|
|
$items = (array)$driverResponse->get(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* getItems() may provides expired item(s) |
60
|
|
|
* themselves provided by a cache of item |
61
|
|
|
* keys based stored the tag item. |
62
|
|
|
* Therefore we pass a filter callback |
63
|
|
|
* to remove the expired Item(s) provided by |
64
|
|
|
* the item keys passed through getItems() |
65
|
|
|
* |
66
|
|
|
* #headache |
67
|
|
|
*/ |
68
|
|
|
return array_filter($this->getItems(array_unique(array_keys($items))), function (ExtendedCacheItemInterface $item) { |
69
|
|
|
return $item->isHit(); |
70
|
|
|
}); |
71
|
|
|
} else { |
72
|
|
|
return []; |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
|
|
public function getItemsByTags(array $tagNames) |
83
|
|
|
{ |
84
|
|
|
$items = []; |
85
|
|
|
foreach (array_unique($tagNames) as $tagName) { |
86
|
|
|
if(is_string($tagName)){ |
87
|
|
|
$items = array_merge($items, $this->getItemsByTag($tagName)); |
88
|
|
|
}else{ |
89
|
|
|
throw new InvalidArgumentException('$tagName must be a a string'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $items; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function getItemsByTagsAll(array $tagNames) |
101
|
|
|
{ |
102
|
|
|
$items = $this->getItemsByTags($tagNames); |
103
|
|
|
|
104
|
|
|
foreach ($items as $key => $item) { |
105
|
|
|
if (array_diff($tagNames, $item->getTags())) { |
106
|
|
|
unset($items[ $key ]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $items; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512) |
118
|
|
|
{ |
119
|
|
|
$callback = function (CacheItemInterface $item) { |
120
|
|
|
return $item->get(); |
121
|
|
|
}; |
122
|
|
|
|
123
|
|
|
return json_encode(array_map($callback, array_values($this->getItemsByTags($tagNames))), $option, $depth); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
|
|
public function deleteItemsByTag($tagName) |
130
|
|
|
{ |
131
|
|
|
if (is_string($tagName)) { |
132
|
|
|
$return = null; |
133
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
134
|
|
|
$result = $this->deleteItem($item->getKey()); |
135
|
|
|
if ($return !== false) { |
136
|
|
|
$return = $result; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $return; |
141
|
|
|
} else { |
142
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
|
|
public function deleteItemsByTags(array $tagNames) |
150
|
|
|
{ |
151
|
|
|
$return = null; |
152
|
|
|
foreach ($tagNames as $tagName) { |
153
|
|
|
$result = $this->deleteItemsByTag($tagName); |
154
|
|
|
if ($return !== false) { |
155
|
|
|
$return = $result; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
|
public function deleteItemsByTagsAll(array $tagNames) |
166
|
|
|
{ |
167
|
|
|
$return = null; |
168
|
|
|
$items = $this->getItemsByTagsAll($tagNames); |
169
|
|
|
|
170
|
|
|
foreach ($items as $key => $item) { |
171
|
|
|
$result = $this->deleteItem($item->getKey()); |
172
|
|
|
if ($return !== false) { |
173
|
|
|
$return = $result; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $return; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritdoc |
182
|
|
|
*/ |
183
|
|
View Code Duplication |
public function incrementItemsByTag($tagName, $step = 1) |
184
|
|
|
{ |
185
|
|
|
if (is_string($tagName) && is_int($step)) { |
186
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
187
|
|
|
$item->increment($step); |
188
|
|
|
$this->saveDeferred($item); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->commit(); |
192
|
|
|
} else { |
193
|
|
|
throw new InvalidArgumentException('$tagName must be a string and $step an integer'); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @inheritdoc |
199
|
|
|
*/ |
200
|
|
|
public function incrementItemsByTags(array $tagNames, $step = 1) |
201
|
|
|
{ |
202
|
|
|
$return = null; |
203
|
|
|
foreach ($tagNames as $tagName) { |
204
|
|
|
$result = $this->incrementItemsByTag($tagName, $step); |
205
|
|
|
if ($return !== false) { |
206
|
|
|
$return = $result; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $return; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritdoc |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
public function incrementItemsByTagsAll(array $tagNames, $step = 1) |
217
|
|
|
{ |
218
|
|
|
if (is_int($step)) { |
219
|
|
|
$items = $this->getItemsByTagsAll($tagNames); |
220
|
|
|
|
221
|
|
|
foreach ($items as $key => $item) { |
222
|
|
|
$item->increment($step); |
223
|
|
|
$this->saveDeferred($item); |
224
|
|
|
} |
225
|
|
|
return $this->commit(); |
226
|
|
|
} else { |
227
|
|
|
throw new InvalidArgumentException('$step must be an integer'); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @inheritdoc |
233
|
|
|
*/ |
234
|
|
View Code Duplication |
public function decrementItemsByTag($tagName, $step = 1) |
235
|
|
|
{ |
236
|
|
|
if (is_string($tagName) && is_int($step)) { |
237
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
238
|
|
|
$item->decrement($step); |
239
|
|
|
$this->saveDeferred($item); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $this->commit(); |
243
|
|
|
} else { |
244
|
|
|
throw new InvalidArgumentException('$tagName must be a string and $step an integer'); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @inheritdoc |
250
|
|
|
*/ |
251
|
|
|
public function decrementItemsByTags(array $tagNames, $step = 1) |
252
|
|
|
{ |
253
|
|
|
$return = null; |
254
|
|
|
foreach ($tagNames as $tagName) { |
255
|
|
|
$result = $this->decrementItemsByTag($tagName, $step); |
256
|
|
|
if ($return !== false) { |
257
|
|
|
$return = $result; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $return; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @inheritdoc |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
public function decrementItemsByTagsAll(array $tagNames, $step = 1) |
268
|
|
|
{ |
269
|
|
|
if (is_int($step)) { |
270
|
|
|
$items = $this->getItemsByTagsAll($tagNames); |
271
|
|
|
|
272
|
|
|
foreach ($items as $key => $item) { |
273
|
|
|
$item->decrement($step); |
274
|
|
|
$this->saveDeferred($item); |
275
|
|
|
} |
276
|
|
|
return $this->commit(); |
277
|
|
|
} else { |
278
|
|
|
throw new InvalidArgumentException('$step must be an integer'); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @inheritdoc |
284
|
|
|
*/ |
285
|
|
View Code Duplication |
public function appendItemsByTag($tagName, $data) |
286
|
|
|
{ |
287
|
|
|
if (is_string($tagName)) { |
288
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
289
|
|
|
$item->append($data); |
290
|
|
|
$this->saveDeferred($item); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $this->commit(); |
294
|
|
|
} else { |
295
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @inheritdoc |
301
|
|
|
*/ |
302
|
|
|
public function appendItemsByTags(array $tagNames, $data) |
303
|
|
|
{ |
304
|
|
|
$return = null; |
305
|
|
|
foreach ($tagNames as $tagName) { |
306
|
|
|
$result = $this->appendItemsByTag($tagName, $data); |
307
|
|
|
if ($return !== false) { |
308
|
|
|
$return = $result; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $return; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @inheritdoc |
317
|
|
|
*/ |
318
|
|
|
public function appendItemsByTagsAll(array $tagNames, $data) |
319
|
|
|
{ |
320
|
|
|
if (is_scalar($data)) { |
321
|
|
|
$items = $this->getItemsByTagsAll($tagNames); |
322
|
|
|
|
323
|
|
|
foreach ($items as $key => $item) { |
324
|
|
|
$item->append($data); |
325
|
|
|
$this->saveDeferred($item); |
326
|
|
|
} |
327
|
|
|
return $this->commit(); |
328
|
|
|
} else { |
329
|
|
|
throw new InvalidArgumentException('$data must be scalar'); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @inheritdoc |
335
|
|
|
*/ |
336
|
|
View Code Duplication |
public function prependItemsByTag($tagName, $data) |
337
|
|
|
{ |
338
|
|
|
if (is_string($tagName)) { |
339
|
|
|
foreach ($this->getItemsByTag($tagName) as $item) { |
340
|
|
|
$item->prepend($data); |
341
|
|
|
$this->saveDeferred($item); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
return $this->commit(); |
345
|
|
|
} else { |
346
|
|
|
throw new InvalidArgumentException('$tagName must be a string'); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @inheritdoc |
352
|
|
|
*/ |
353
|
|
|
public function prependItemsByTags(array $tagNames, $data) |
354
|
|
|
{ |
355
|
|
|
$return = null; |
356
|
|
|
foreach ($tagNames as $tagName) { |
357
|
|
|
$result = $this->prependItemsByTag($tagName, $data); |
358
|
|
|
if ($return !== false) { |
359
|
|
|
$return = $result; |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return $return; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @inheritdoc |
368
|
|
|
*/ |
369
|
|
|
public function prependItemsByTagsAll(array $tagNames, $data) |
370
|
|
|
{ |
371
|
|
|
if (is_scalar($data)) { |
372
|
|
|
$items = $this->getItemsByTagsAll($tagNames); |
373
|
|
|
|
374
|
|
|
foreach ($items as $key => $item) { |
375
|
|
|
$item->prepend($data); |
376
|
|
|
$this->saveDeferred($item); |
377
|
|
|
} |
378
|
|
|
return $this->commit(); |
379
|
|
|
} else { |
380
|
|
|
throw new InvalidArgumentException('$data must be scalar'); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
386
|
|
|
* @return void |
387
|
|
|
*/ |
388
|
|
|
public function detachItem(CacheItemInterface $item) |
389
|
|
|
{ |
390
|
|
|
if (isset($this->itemInstances[ $item->getKey() ])) { |
391
|
|
|
$this->deregisterItem($item); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @inheritdoc |
397
|
|
|
*/ |
398
|
|
|
public function detachAllItems() |
399
|
|
|
{ |
400
|
|
|
foreach ($this->itemInstances as $item) { |
401
|
|
|
$this->detachItem($item); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @inheritdoc |
407
|
|
|
*/ |
408
|
|
|
public function attachItem(CacheItemInterface $item) |
409
|
|
|
{ |
410
|
|
View Code Duplication |
if (isset($this->itemInstances[ $item->getKey() ]) && spl_object_hash($item) !== spl_object_hash($this->itemInstances[ $item->getKey() ])) { |
411
|
|
|
throw new \LogicException('The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.'); |
412
|
|
|
} else { |
413
|
|
|
$this->itemInstances[ $item->getKey() ] = $item; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @internal This method de-register an item from $this->itemInstances |
420
|
|
|
* @param CacheItemInterface|string $item |
421
|
|
|
* @throws phpFastCacheInvalidArgumentException |
422
|
|
|
*/ |
423
|
|
|
protected function deregisterItem($item) |
424
|
|
|
{ |
425
|
|
|
if ($item instanceof CacheItemInterface) { |
426
|
|
|
unset($this->itemInstances[ $item->getKey() ]); |
427
|
|
|
|
428
|
|
|
} else if (is_string($item)) { |
429
|
|
|
unset($this->itemInstances[ $item ]); |
430
|
|
|
} else { |
431
|
|
|
throw new phpFastCacheInvalidArgumentException('Invalid type for $item variable'); |
432
|
|
|
} |
433
|
|
|
if (gc_enabled()) { |
434
|
|
|
gc_collect_cycles(); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Returns true if the item exists, is attached and the Spl Hash matches |
440
|
|
|
* Returns false if the item exists, is attached and the Spl Hash mismatches |
441
|
|
|
* Returns null if the item does not exists |
442
|
|
|
* |
443
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
444
|
|
|
* @return bool|null |
445
|
|
|
* @throws \LogicException |
446
|
|
|
*/ |
447
|
|
View Code Duplication |
public function isAttached(CacheItemInterface $item) |
448
|
|
|
{ |
449
|
|
|
if (isset($this->itemInstances[ $item->getKey() ])) { |
450
|
|
|
return spl_object_hash($item) === spl_object_hash($this->itemInstances[ $item->getKey() ]); |
451
|
|
|
} |
452
|
|
|
return null; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Set the EventManager instance |
457
|
|
|
* |
458
|
|
|
* @param EventManager $em |
459
|
|
|
*/ |
460
|
|
|
public function setEventManager(EventManager $em) |
461
|
|
|
{ |
462
|
|
|
$this->eventManager = $em; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @inheritdoc |
467
|
|
|
*/ |
468
|
|
|
public function saveMultiple(...$items) |
469
|
|
|
{ |
470
|
|
|
if (isset($items[ 0 ]) && is_array($items[ 0 ])) { |
471
|
|
|
foreach ($items[ 0 ] as $item) { |
472
|
|
|
$this->save($item); |
473
|
|
|
} |
474
|
|
|
return true; |
475
|
|
|
} else if (is_array($items)) { |
476
|
|
|
foreach ($items as $item) { |
477
|
|
|
$this->save($item); |
478
|
|
|
} |
479
|
|
|
return true; |
480
|
|
|
} |
481
|
|
|
return false; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @return string |
486
|
|
|
*/ |
487
|
|
|
public static function getHelp() |
488
|
|
|
{ |
489
|
|
|
return ''; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Driver-related methods |
494
|
|
|
*/ |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
498
|
|
|
* @return array [ |
499
|
|
|
* 'd' => 'THE ITEM DATA' |
500
|
|
|
* 't' => 'THE ITEM DATE EXPIRATION' |
501
|
|
|
* 'g' => 'THE ITEM TAGS' |
502
|
|
|
* ] |
503
|
|
|
* |
504
|
|
|
*/ |
505
|
|
|
abstract protected function driverRead(CacheItemInterface $item); |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
509
|
|
|
* @return mixed |
510
|
|
|
*/ |
511
|
|
|
abstract protected function driverWrite(CacheItemInterface $item); |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* @return bool |
515
|
|
|
*/ |
516
|
|
|
abstract protected function driverClear(); |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @return bool |
520
|
|
|
*/ |
521
|
|
|
abstract protected function driverConnect(); |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
525
|
|
|
* @return bool |
526
|
|
|
*/ |
527
|
|
|
abstract protected function driverDelete(CacheItemInterface $item); |
528
|
|
|
} |