diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Cache/Region/DefaultRegion.php')
-rw-r--r-- | vendor/doctrine/orm/src/Cache/Region/DefaultRegion.php | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Cache/Region/DefaultRegion.php b/vendor/doctrine/orm/src/Cache/Region/DefaultRegion.php new file mode 100644 index 0000000..0576195 --- /dev/null +++ b/vendor/doctrine/orm/src/Cache/Region/DefaultRegion.php | |||
@@ -0,0 +1,113 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Cache\Region; | ||
6 | |||
7 | use Doctrine\ORM\Cache\CacheEntry; | ||
8 | use Doctrine\ORM\Cache\CacheKey; | ||
9 | use Doctrine\ORM\Cache\CollectionCacheEntry; | ||
10 | use Doctrine\ORM\Cache\Lock; | ||
11 | use Doctrine\ORM\Cache\Region; | ||
12 | use Psr\Cache\CacheItemInterface; | ||
13 | use Psr\Cache\CacheItemPoolInterface; | ||
14 | use Traversable; | ||
15 | |||
16 | use function array_map; | ||
17 | use function iterator_to_array; | ||
18 | use function strtr; | ||
19 | |||
20 | /** | ||
21 | * The simplest cache region compatible with all doctrine-cache drivers. | ||
22 | */ | ||
23 | class DefaultRegion implements Region | ||
24 | { | ||
25 | private const REGION_KEY_SEPARATOR = '_'; | ||
26 | private const REGION_PREFIX = 'DC2_REGION_'; | ||
27 | |||
28 | public function __construct( | ||
29 | private readonly string $name, | ||
30 | private readonly CacheItemPoolInterface $cacheItemPool, | ||
31 | private readonly int $lifetime = 0, | ||
32 | ) { | ||
33 | } | ||
34 | |||
35 | public function getName(): string | ||
36 | { | ||
37 | return $this->name; | ||
38 | } | ||
39 | |||
40 | public function contains(CacheKey $key): bool | ||
41 | { | ||
42 | return $this->cacheItemPool->hasItem($this->getCacheEntryKey($key)); | ||
43 | } | ||
44 | |||
45 | public function get(CacheKey $key): CacheEntry|null | ||
46 | { | ||
47 | $item = $this->cacheItemPool->getItem($this->getCacheEntryKey($key)); | ||
48 | $entry = $item->isHit() ? $item->get() : null; | ||
49 | |||
50 | if (! $entry instanceof CacheEntry) { | ||
51 | return null; | ||
52 | } | ||
53 | |||
54 | return $entry; | ||
55 | } | ||
56 | |||
57 | public function getMultiple(CollectionCacheEntry $collection): array|null | ||
58 | { | ||
59 | $keys = array_map( | ||
60 | $this->getCacheEntryKey(...), | ||
61 | $collection->identifiers, | ||
62 | ); | ||
63 | /** @var iterable<string, CacheItemInterface> $items */ | ||
64 | $items = $this->cacheItemPool->getItems($keys); | ||
65 | if ($items instanceof Traversable) { | ||
66 | $items = iterator_to_array($items); | ||
67 | } | ||
68 | |||
69 | $result = []; | ||
70 | foreach ($keys as $arrayKey => $cacheKey) { | ||
71 | if (! isset($items[$cacheKey]) || ! $items[$cacheKey]->isHit()) { | ||
72 | return null; | ||
73 | } | ||
74 | |||
75 | $entry = $items[$cacheKey]->get(); | ||
76 | if (! $entry instanceof CacheEntry) { | ||
77 | return null; | ||
78 | } | ||
79 | |||
80 | $result[$arrayKey] = $entry; | ||
81 | } | ||
82 | |||
83 | return $result; | ||
84 | } | ||
85 | |||
86 | public function put(CacheKey $key, CacheEntry $entry, Lock|null $lock = null): bool | ||
87 | { | ||
88 | $item = $this->cacheItemPool | ||
89 | ->getItem($this->getCacheEntryKey($key)) | ||
90 | ->set($entry); | ||
91 | |||
92 | if ($this->lifetime > 0) { | ||
93 | $item->expiresAfter($this->lifetime); | ||
94 | } | ||
95 | |||
96 | return $this->cacheItemPool->save($item); | ||
97 | } | ||
98 | |||
99 | public function evict(CacheKey $key): bool | ||
100 | { | ||
101 | return $this->cacheItemPool->deleteItem($this->getCacheEntryKey($key)); | ||
102 | } | ||
103 | |||
104 | public function evictAll(): bool | ||
105 | { | ||
106 | return $this->cacheItemPool->clear(self::REGION_PREFIX . $this->name); | ||
107 | } | ||
108 | |||
109 | private function getCacheEntryKey(CacheKey $key): string | ||
110 | { | ||
111 | return self::REGION_PREFIX . $this->name . self::REGION_KEY_SEPARATOR . strtr($key->hash, '{}()/\@:', '________'); | ||
112 | } | ||
113 | } | ||