|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of phpFastCache. |
|
6
|
|
|
* |
|
7
|
|
|
* @license MIT License (MIT) |
|
8
|
|
|
* |
|
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> https://www.phpfastcache.com |
|
12
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
declare(strict_types=1); |
|
16
|
|
|
|
|
17
|
|
|
namespace Phpfastcache\Core\Pool; |
|
18
|
|
|
|
|
19
|
|
|
use Phpfastcache\Entities\DriverIO; |
|
20
|
|
|
use Phpfastcache\Exceptions\{PhpfastcacheLogicException}; |
|
21
|
|
|
use Psr\Cache\CacheItemInterface; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Trait ExtendedCacheItemPoolTrait |
|
26
|
|
|
* @package Phpfastcache\Core\Pool |
|
27
|
|
|
*/ |
|
28
|
|
|
trait ExtendedCacheItemPoolTrait |
|
29
|
|
|
{ |
|
30
|
|
|
use CacheItemPoolTrait; |
|
31
|
|
|
use AbstractDriverPoolTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var DriverIO |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $IO; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getItemsAsJsonString(array $keys = [], int $option = 0, int $depth = 512): string |
|
42
|
|
|
{ |
|
43
|
|
|
$callback = static function (CacheItemInterface $item) { |
|
44
|
|
|
return $item->get(); |
|
45
|
|
|
}; |
|
46
|
|
|
return \json_encode(\array_map($callback, \array_values($this->getItems($keys))), $option, $depth); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function detachAllItems() |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($this->itemInstances as $item) { |
|
55
|
|
|
$this->detachItem($item); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param CacheItemInterface $item |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function detachItem(CacheItemInterface $item) |
|
64
|
|
|
{ |
|
65
|
|
|
if (isset($this->itemInstances[$item->getKey()])) { |
|
66
|
|
|
$this->deregisterItem($item->getKey()); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $item |
|
72
|
|
|
* @internal This method de-register an item from $this->itemInstances |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function deregisterItem(string $item) |
|
75
|
|
|
{ |
|
76
|
|
|
unset($this->itemInstances[$item]); |
|
77
|
|
|
|
|
78
|
|
|
if (\gc_enabled()) { |
|
79
|
|
|
\gc_collect_cycles(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
|
|
public function attachItem(CacheItemInterface $item) |
|
87
|
|
|
{ |
|
88
|
|
|
if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
89
|
|
|
throw new PhpfastcacheLogicException( |
|
90
|
|
|
'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.' |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->itemInstances[$item->getKey()] = $item; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns true if the item exists, is attached and the Spl Hash matches |
|
99
|
|
|
* Returns false if the item exists, is attached and the Spl Hash mismatches |
|
100
|
|
|
* Returns null if the item does not exists |
|
101
|
|
|
* |
|
102
|
|
|
* @param CacheItemInterface $item |
|
103
|
|
|
* @return bool|null |
|
104
|
|
|
*/ |
|
105
|
|
|
public function isAttached(CacheItemInterface $item) |
|
106
|
|
|
{ |
|
107
|
|
|
if (isset($this->itemInstances[$item->getKey()])) { |
|
108
|
|
|
return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
109
|
|
|
} |
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @inheritdoc |
|
115
|
|
|
*/ |
|
116
|
|
|
public function saveMultiple(...$items): bool |
|
117
|
|
|
{ |
|
118
|
|
|
if (isset($items[0]) && \is_array($items[0])) { |
|
119
|
|
|
foreach ($items[0] as $item) { |
|
120
|
|
|
$this->save($item); |
|
121
|
|
|
} |
|
122
|
|
|
return true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (\is_array($items)) { |
|
126
|
|
|
foreach ($items as $item) { |
|
127
|
|
|
$this->save($item); |
|
128
|
|
|
} |
|
129
|
|
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return DriverIO |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getIO(): DriverIO |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->IO; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getHelp(): string |
|
146
|
|
|
{ |
|
147
|
|
|
return ''; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|