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