-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathArrayCachePool.php
More file actions
292 lines (237 loc) · 7.72 KB
/
Copy pathArrayCachePool.php
File metadata and controls
292 lines (237 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/*
* This file is part of php-cache organization.
*
* (c) 2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Cache\Adapter\PHPArray;
use Cache\Adapter\Common\AbstractCachePool;
use Cache\Adapter\Common\PhpCacheItem;
use Cache\Hierarchy\HierarchicalCachePoolTrait;
use Cache\Hierarchy\HierarchicalPoolInterface;
/**
* Array cache pool. You could set a limit of how many items you want to be stored to avoid memory leaks.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterface
{
use HierarchicalCachePoolTrait;
/** @var array<array-key, mixed> */
private array $cache;
/** @var array<int, list<string>> */
private array $keyMap = [];
private ?int $limit;
private int $currentPosition = 0;
/**
* @param array<array-key, mixed> $cache
*/
public function __construct(?int $limit = null, array &$cache = [])
{
$this->cache = &$cache;
$this->limit = $limit;
}
/** @return PhpCacheItem|array{bool, mixed, list<array{0: string, 1: string}>, int|null} */
protected function getItemWithoutGenerateCacheKey(string $key): PhpCacheItem|array
{
if (isset($this->deferred[$key])) {
$item = clone $this->deferred[$key];
$item->moveTagsToPrevious();
return $item;
}
return $this->fetchObjectFromCache($key);
}
/** @return array{bool, mixed, list<array{0: string, 1: string}>, int|null} */
protected function fetchObjectFromCache(string $key): array
{
$keys = $this->getHierarchyKey($key);
if (!$this->cacheIsset($keys)) {
return [false, null, [], null];
}
$element = $this->decodeCacheElement($this->cacheToolkit($keys));
if (null === $element) {
return [false, null, [], null];
}
[$data, $tags, $timestamp] = $element;
if (\is_object($data)) {
$data = clone $data;
}
return [true, $data, $tags, $timestamp];
}
protected function clearAllObjectsFromCache(): bool
{
$this->cache = [];
$this->keyMap = [];
$this->currentPosition = 0;
$this->clearHierarchyKeyCache();
return true;
}
protected function clearOneObjectFromCache(string $key): bool
{
$this->commit();
$keys = $this->getHierarchyKey($key);
$this->clearHierarchyKeyCache();
$this->cacheToolkit($keys, null, true);
foreach ($this->keyMap as $position => $trackedKeys) {
if ($trackedKeys === $keys) {
unset($this->keyMap[$position]);
}
}
return true;
}
protected function storeItemInCache(PhpCacheItem $item, ?int $ttl): bool
{
$keys = $this->getHierarchyKey($item->getKey());
$value = $item->get();
if (\is_object($value)) {
$value = clone $value;
}
if (null !== $this->limit) {
$tracked = false;
foreach ($this->keyMap as $trackedKeys) {
if ($trackedKeys === $keys) {
$tracked = true;
break;
}
}
if (!$tracked) {
if (isset($this->keyMap[$this->currentPosition])) {
$this->cacheToolkit($this->keyMap[$this->currentPosition], null, true);
}
$this->keyMap[$this->currentPosition] = $keys;
$this->currentPosition = ($this->currentPosition + 1) % $this->limit;
}
}
$this->cacheToolkit($keys, [$value, $item->getTagVersions(), $item->getExpirationTimestamp()]);
return true;
}
public function getDirectValue(string $key): mixed
{
return $this->cache[$key] ?? null;
}
/** @return list<string> */
protected function getList(string $name): array
{
$data = $this->cache[$name] ?? [];
if (!\is_array($data)) {
return [];
}
$list = [];
foreach ($data as $value) {
if (!\is_string($value)) {
return [];
}
$list[] = $value;
}
return $list;
}
protected function removeList(string $name): bool
{
unset($this->cache[$name]);
return true;
}
protected function appendListItem(string $name, string $key): bool
{
$list = $this->getList($name);
$list[] = $key;
$this->cache[$name] = $list;
return true;
}
protected function removeListItem(string $name, string $key): bool
{
$list = $this->getList($name);
foreach ($list as $i => $item) {
if ($item === $key) {
unset($list[$i]);
}
}
$this->cache[$name] = array_values($list);
return true;
}
/**
* Used to manipulate cached data by extracting, inserting or deleting value.
*
* @param list<string> $keys
*/
private function cacheToolkit(array $keys, mixed $value = null, bool $unset = false): mixed
{
$element = &$this->cache;
while ([] !== $keys) {
$key = array_shift($keys);
if (!\is_array($element)) {
$element = [];
}
if (!$keys && null === $value && $unset) {
unset($element[$key]);
unset($element);
$element = null;
} else {
$element = &$element[$key];
}
}
if (!$unset && null !== $value) {
$element = $value;
}
return $element;
}
/**
* Checking if given keys exists and is valid.
*
* @param list<string> $keys
*/
private function cacheIsset(array $keys): bool
{
$data = $this->cache;
foreach ($keys as $key) {
if (!\is_array($data) || !\array_key_exists($key, $data)) {
return false;
}
$data = $data[$key];
}
return \is_array($data) && \array_key_exists(0, $data);
}
/**
* Get a key to use with the hierarchy. If the key does not start with HierarchicalPoolInterface::SEPARATOR
* this will return an unalterered key. This function supports a tagged key. Ie "foo:bar".
* With this overwrite we'll return array as keys.
*
* @param string $key The original key
*
* @return list<string>
*/
protected function getHierarchyKey(string $key, ?string &$pathKey = null): array
{
if (!$this->isHierarchyKey($key)) {
return [$key];
}
return $this->explodeKey($key);
}
/** @return array{mixed, list<array{0: string, 1: string}>, int|null}|null */
private function decodeCacheElement(mixed $element): ?array
{
if (!\is_array($element)
|| !\array_key_exists(0, $element)
|| !\array_key_exists(1, $element)
|| !\array_key_exists(2, $element)
|| !\is_array($element[1])
|| (!\is_int($element[2]) && null !== $element[2])
) {
return null;
}
$tags = [];
foreach ($element[1] as $tagVersion) {
if (!\is_array($tagVersion) || !array_is_list($tagVersion) || 2 !== \count($tagVersion)) {
return null;
}
[$tag, $version] = $tagVersion;
if (!\is_string($tag) || !\is_string($version)) {
return null;
}
$tags[] = [$tag, $version];
}
return [$element[0], $tags, $element[2]];
}
}