-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamespacedCachePool.php
More file actions
497 lines (404 loc) · 15.5 KB
/
Copy pathNamespacedCachePool.php
File metadata and controls
497 lines (404 loc) · 15.5 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?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\Namespaced;
use Cache\Hierarchy\HierarchicalPoolInterface;
use Cache\Namespaced\Exception\CacheException;
use Cache\Namespaced\Exception\InvalidArgumentException;
use Cache\TagInterop\TaggableCacheItemInterface;
use Cache\TagInterop\TaggableCacheItemPoolInterface;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
/**
* Prefix all the stored items with a namespace. Also make sure you can clear all items
* in that namespace.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class NamespacedCachePool implements HierarchicalPoolInterface
{
private const GENERATION_METADATA_TYPE = 'php-cache.namespaced-generation';
private const GENERATION_METADATA_VERSION = 1;
private const GENERATION_PROBE_LIMIT = 100;
private const ENCODING_MARKER = '_x';
private const HIERARCHY_MARKER = '_x7C_';
private CacheItemPoolInterface $cachePool;
private string $namespace;
/** @var list<string> */
private array $namespaceParts;
private readonly object $owner;
private readonly NamespacedTagMapper $tagMapper;
private bool $usesHierarchy;
/**
* @return ($cachePool is TaggableCacheItemPoolInterface ? TaggableNamespacedCachePool : self)
*/
public static function create(CacheItemPoolInterface $cachePool, string $namespace): self
{
if ($cachePool instanceof TaggableCacheItemPoolInterface) {
return new TaggableNamespacedCachePool($cachePool, $namespace);
}
return new self($cachePool, $namespace);
}
public function __construct(CacheItemPoolInterface $cachePool, string $namespace)
{
if ('' === $namespace) {
throw new InvalidArgumentException('Cache namespace cannot be an empty string.');
}
$namespace = $this->encodeNamespaceComponent($namespace);
if ($cachePool instanceof self) {
$this->cachePool = $cachePool->cachePool;
$this->namespaceParts = [...$cachePool->namespaceParts, $namespace];
$this->usesHierarchy = $cachePool->usesHierarchy;
} else {
$this->cachePool = $cachePool;
$this->namespaceParts = [$namespace];
$this->usesHierarchy = $cachePool instanceof HierarchicalPoolInterface;
}
$namespaceBoundary = str_repeat(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, 2);
$this->namespace = implode($namespaceBoundary, $this->namespaceParts);
$this->owner = new \stdClass();
$this->tagMapper = new NamespacedTagMapper($this->namespace);
}
/**
* Add namespace prefix on the key.
*/
private function prefixValue(string &$key): void
{
$key = $this->encodeKey($key);
$this->prefixEncodedValue($key);
}
private function prefixEncodedValue(string &$key): void
{
if ($this->usesHierarchy) {
$separator = HierarchicalPoolInterface::HIERARCHY_SEPARATOR;
if ($this->isHierarchyKey($key)) {
$key = self::HIERARCHY_MARKER.($separator === $key ? '' : $key);
}
$key = $separator.$this->namespace.$separator.$key;
return;
}
$key = 'ns.i.'.sha1($this->getGenerationPath($key)."\0".$key);
}
private function getGenerationPath(string $key): string
{
$generations = [];
foreach ($this->getGenerationPaths($key) as $path) {
[$item, $generation] = $this->findGenerationMetadata($path);
if (null === $generation) {
$generation = bin2hex(random_bytes(16));
if (!$this->cachePool->save($item->set($this->createGenerationMetadata($path, $generation)))) {
throw new CacheException('Could not persist namespace generation metadata.');
}
}
$generations[] = $generation;
}
return $this->namespace."\0".implode("\0", $generations);
}
/**
* @return list<string>
*/
private function getGenerationPaths(string $key = ''): array
{
$path = '';
$paths = [];
foreach ($this->namespaceParts as $index => $namespace) {
$path .= str_repeat(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, 0 === $index ? 1 : 2).$namespace;
$paths[] = $path;
}
if ($this->isHierarchyKey($key)) {
$path .= HierarchicalPoolInterface::HIERARCHY_SEPARATOR.self::HIERARCHY_MARKER;
$paths[] = $path;
if (HierarchicalPoolInterface::HIERARCHY_SEPARATOR === $key) {
return $paths;
}
foreach (\array_slice(explode(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, $key), 1) as $component) {
$path .= HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$component;
$paths[] = $path;
}
}
return $paths;
}
private function getHierarchyGenerationPath(string $key): string
{
$path = HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$this->namespace
.HierarchicalPoolInterface::HIERARCHY_SEPARATOR.self::HIERARCHY_MARKER;
if (HierarchicalPoolInterface::HIERARCHY_SEPARATOR === $key) {
return $path;
}
foreach (\array_slice(explode(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, $key), 1) as $component) {
$path .= HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$component;
}
return $path;
}
private function getGenerationKey(string $path, int $probe = 0): string
{
return 'ns.g.'.sha1(0 === $probe ? $path : $path."\0".$probe);
}
/**
* @return array{CacheItemInterface, string|null}
*/
private function findGenerationMetadata(string $path): array
{
$availableItem = null;
for ($probe = 0; $probe < self::GENERATION_PROBE_LIMIT; ++$probe) {
$item = $this->cachePool->getItem($this->getGenerationKey($path, $probe));
if (!$item->isHit()) {
$availableItem ??= $item;
continue;
}
$generation = $this->readGenerationMetadata($item->get(), $path);
if (null !== $generation) {
return [$item, $generation];
}
}
if (null !== $availableItem) {
return [$availableItem, null];
}
throw new CacheException('Could not allocate namespace generation metadata without overwriting an existing cache item.');
}
/**
* @return array{type: string, version: int, path: string, generation: string}
*/
private function createGenerationMetadata(string $path, string $generation): array
{
return [
'type' => self::GENERATION_METADATA_TYPE,
'version' => self::GENERATION_METADATA_VERSION,
'path' => $path,
'generation' => $generation,
];
}
private function readGenerationMetadata(mixed $metadata, string $path): ?string
{
if (!\is_array($metadata)
|| self::GENERATION_METADATA_TYPE !== ($metadata['type'] ?? null)
|| self::GENERATION_METADATA_VERSION !== ($metadata['version'] ?? null)
|| $path !== ($metadata['path'] ?? null)
|| !\is_string($metadata['generation'] ?? null)
) {
return null;
}
return $metadata['generation'];
}
private function encodeNamespaceComponent(string $component): string
{
$encoded = '';
$length = \strlen($component);
for ($index = 0; $index < $length; ++$index) {
$byte = $component[$index];
$ordinal = \ord($byte);
$portable = ($ordinal >= 48 && $ordinal <= 57)
|| ($ordinal >= 65 && $ordinal <= 90)
|| ($ordinal >= 97 && $ordinal <= 122)
|| '_' === $byte
|| '.' === $byte;
$startsMarker = '_' === $byte && 'x' === ($component[$index + 1] ?? null);
$encoded .= $portable && !$startsMarker ? $byte : $this->encodeByte($byte);
}
return $encoded;
}
private function encodeKeyComponent(string $component): string
{
$encoded = '';
$length = \strlen($component);
for ($index = 0; $index < $length; ++$index) {
$byte = $component[$index];
$startsMarker = '_' === $byte && 'x' === ($component[$index + 1] ?? null);
$encoded .= '|' !== $byte && '!' !== $byte && !$startsMarker ? $byte : $this->encodeByte($byte);
}
return $encoded;
}
private function encodeByte(string $byte): string
{
return self::ENCODING_MARKER.strtoupper(bin2hex($byte)).'_';
}
private function encodeKey(string $key): string
{
$this->validateKey($key);
if (!$this->isHierarchyKey($key)) {
return $this->encodeKeyComponent($key);
}
return implode(
HierarchicalPoolInterface::HIERARCHY_SEPARATOR,
array_map($this->encodeKeyComponent(...), explode(HierarchicalPoolInterface::HIERARCHY_SEPARATOR, $key))
);
}
private function isHierarchyKey(string $key): bool
{
return HierarchicalPoolInterface::HIERARCHY_SEPARATOR === ($key[0] ?? null);
}
private function validateKey(string $key): void
{
if ('' === $key) {
throw new InvalidArgumentException('Cache key cannot be an empty string');
}
if (preg_match('|[\{\}\(\)/\\\@\:]|', $key)) {
throw new InvalidArgumentException(\sprintf('Invalid key: "%s". The key contains one or more characters reserved for future extension: {}()/\@:', $key));
}
}
/**
* @param array<array-key, mixed> $keys
*
* @return array<array-key, string>
*/
private function prefixValues(array $keys): array
{
$prefixedKeys = [];
foreach ($this->encodeValues($keys) as $index => $key) {
$this->prefixEncodedValue($key);
$prefixedKeys[$index] = $key;
}
return $prefixedKeys;
}
/**
* @param array<array-key, mixed> $keys
*
* @return array<array-key, string>
*/
private function encodeValues(array $keys): array
{
foreach ($keys as $index => $key) {
if (!\is_string($key)) {
throw new InvalidArgumentException(\sprintf('Cache key must be string, "%s" given', get_debug_type($key)));
}
$keys[$index] = $this->encodeKey($key);
}
return $keys;
}
public function getItem(string $key): CacheItemInterface
{
$originalKey = $key;
$this->prefixValue($key);
return $this->wrapItem($originalKey, $this->cachePool->getItem($key));
}
/**
* @param array<array-key, string> $keys
*
* @return iterable<string, CacheItemInterface>
*/
public function getItems(array $keys = []): iterable
{
$prefixedKeys = $this->prefixValues($keys);
$originalKeys = [];
foreach ($prefixedKeys as $index => $prefixedKey) {
$originalKeys["\0".$prefixedKey] = $keys[$index];
}
return $this->wrapItems($prefixedKeys, $originalKeys);
}
/**
* @param array<array-key, string> $prefixedKeys
* @param array<string, string> $originalKeys
*
* @return \Generator<string, CacheItemInterface>
*/
private function wrapItems(array $prefixedKeys, array $originalKeys): \Generator
{
foreach ($this->cachePool->getItems($prefixedKeys) as $item) {
$mappedKey = "\0".$item->getKey();
if (!\array_key_exists($mappedKey, $originalKeys)) {
continue;
}
$originalKey = $originalKeys[$mappedKey];
yield $originalKey => $this->wrapItem($originalKey, $item);
}
}
private function wrapItem(string $key, CacheItemInterface $item): NamespacedCacheItem
{
if ($item instanceof TaggableCacheItemInterface) {
return new TaggableNamespacedCacheItem($key, $item, $this->owner, $this->tagMapper);
}
return new NamespacedCacheItem($key, $item, $this->owner);
}
protected function mapTag(string $tag): string
{
return $this->tagMapper->map($tag);
}
/**
* @param array<array-key, string> $tags
*
* @return array<array-key, string>
*/
protected function mapTags(array $tags): array
{
return $this->tagMapper->mapTags($tags);
}
public function hasItem(string $key): bool
{
$this->prefixValue($key);
return $this->cachePool->hasItem($key);
}
public function clear(): bool
{
if ($this->usesHierarchy) {
return $this->cachePool->deleteItem(HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$this->namespace);
}
return $this->advanceGeneration(HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$this->namespace);
}
public function deleteItem(string $key): bool
{
if (!$this->usesHierarchy) {
$key = $this->encodeKey($key);
if ($this->isHierarchyKey($key)) {
return $this->advanceGeneration($this->getHierarchyGenerationPath($key));
}
$this->prefixEncodedValue($key);
return $this->cachePool->deleteItem($key);
}
$this->prefixValue($key);
return $this->cachePool->deleteItem($key);
}
public function deleteItems(array $keys): bool
{
if (!$this->usesHierarchy) {
$encodedKeys = $this->encodeValues($keys);
$backendKeys = [];
$generationPaths = [];
foreach ($encodedKeys as $key) {
if ($this->isHierarchyKey($key)) {
$generationPath = $this->getHierarchyGenerationPath($key);
$generationPaths["\0".$generationPath] = $generationPath;
continue;
}
$this->prefixEncodedValue($key);
$backendKeys[] = $key;
}
$deleted = true;
foreach ($generationPaths as $path) {
$deleted = $this->advanceGeneration($path) && $deleted;
}
return ([] === $backendKeys || $this->cachePool->deleteItems($backendKeys)) && $deleted;
}
$keys = $this->prefixValues($keys);
return $this->cachePool->deleteItems($keys);
}
private function advanceGeneration(string $path): bool
{
[$item] = $this->findGenerationMetadata($path);
$generation = bin2hex(random_bytes(16));
return $this->cachePool->save($item->set($this->createGenerationMetadata($path, $generation)));
}
public function save(CacheItemInterface $item): bool
{
if (!$item instanceof NamespacedCacheItem || !$item->isOwnedBy($this->owner)) {
throw new InvalidArgumentException('Cache items are not transferable between pools.');
}
return $this->cachePool->save($item->unwrap());
}
public function saveDeferred(CacheItemInterface $item): bool
{
if (!$item instanceof NamespacedCacheItem || !$item->isOwnedBy($this->owner)) {
throw new InvalidArgumentException('Cache items are not transferable between pools.');
}
return $this->cachePool->saveDeferred($item->unwrap());
}
public function commit(): bool
{
return $this->cachePool->commit();
}
}