Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

AbstractInMemoryHandler.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Cache;
8
9 use eZ\Publish\Core\Persistence\Cache\Adapter\TransactionAwareAdapterInterface;
10 use eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache;
11
18 abstract class AbstractInMemoryHandler
19 {
25 protected $cache;
26
28 protected $logger;
29
36 private $inMemory;
37
45 public function __construct(
46 TransactionAwareAdapterInterface $cache,
47 PersistenceLogger $logger,
48 InMemoryCache $inMemory
49 ) {
50 $this->cache = $cache;
51 $this->logger = $logger;
52 $this->inMemory = $inMemory;
53 }
54
71 final protected function getCacheValue(
72 $id,
73 string $keyPrefix,
74 callable $backendLoader,
75 callable $cacheTagger,
76 callable $cacheIndexes,
77 string $keySuffix = '',
78 array $arguments = []
79 ) {
80 $key = $keyPrefix . $id . $keySuffix;
81 // In-memory
82 if ($object = $this->inMemory->get($key)) {
83 $this->logger->logCacheHit($arguments ?: [$id], 3, true);
84
85 return $object;
86 }
87
88 // Cache pool
89 $cacheItem = $this->cache->getItem($key);
90 if ($cacheItem->isHit()) {
91 $this->logger->logCacheHit($arguments ?: [$id], 3);
92 $this->inMemory->setMulti([$object = $cacheItem->get()], $cacheIndexes);
93
94 return $object;
95 }
96
97 // Backend
98 // (log misses first in case of $backendLoader ends up throwing NotFound)
99 $this->logger->logCacheMiss($arguments ?: [$id], 3);
100
101 $object = $backendLoader($id);
102 $this->inMemory->setMulti([$object], $cacheIndexes);
103 $this->cache->save(
104 $cacheItem
105 ->set($object)
106 ->tag($cacheTagger($object))
107 );
108
109 return $object;
110 }
111
127 final protected function getListCacheValue(
128 string $key,
129 callable $backendLoader,
130 callable $cacheTagger,
131 callable $cacheIndexes,
132 callable $listTags = null,
133 array $arguments = []
134 ) {
135 // In-memory
136 if ($objects = $this->inMemory->get($key)) {
137 $this->logger->logCacheHit($arguments, 3, true);
138
139 return $objects;
140 }
141
142 // Cache pool
143 $cacheItem = $this->cache->getItem($key);
144 if ($cacheItem->isHit()) {
145 $this->logger->logCacheHit($arguments, 3);
146 $this->inMemory->setMulti($objects = $cacheItem->get(), $cacheIndexes, $key);
147
148 return $objects;
149 }
150
151 // Backend
152 // (log misses first in case of $backendLoader ends up throwing NotFound)
153 $this->logger->logCacheMiss($arguments, 3);
154
155 $objects = $backendLoader();
156 $this->inMemory->setMulti($objects, $cacheIndexes, $key);
157 if ($listTags !== null) {
158 $tagSet = [$listTags()];
159 } else {
160 $tagSet = [[]];
161 }
162
163 foreach ($objects as $object) {
164 $tagSet[] = $cacheTagger($object);
165 }
166
167 $this->cache->save(
168 $cacheItem
169 ->set($objects)
170 ->tag(array_unique(array_merge(...$tagSet)))
171 );
172
173 return $objects;
174 }
175
195 final protected function getMultipleCacheValues(
196 array $ids,
197 string $keyPrefix,
198 callable $backendLoader,
199 callable $cacheTagger,
200 callable $cacheIndexes,
201 string $keySuffix = '',
202 array $arguments = []
203 ): array {
204 if (empty($ids)) {
205 return [];
206 }
207
208 // Generate unique cache keys and check if in-memory
209 $list = array_flip($ids);
210 $cacheKeys = [];
211 $cacheKeysToIdMap = [];
212 foreach (array_unique($ids) as $id) {
213 $key = $keyPrefix . $id . $keySuffix;
214 if ($object = $this->inMemory->get($key)) {
215 $list[$id] = $object;
216 } else {
217 $cacheKeys[] = $key;
218 $cacheKeysToIdMap[$key] = $id;
219 }
220 }
221
222 // No in-memory misses
223 if (empty($cacheKeys)) {
224 $this->logger->logCacheHit($arguments ?: $ids, 3, true);
225
226 return $list;
227 }
228
229 // Load cache items by cache keys (will contain hits and misses)
230 $loaded = [];
231 $cacheMisses = [];
232 foreach ($this->cache->getItems($cacheKeys) as $key => $cacheItem) {
233 $id = $cacheKeysToIdMap[$key];
234 if ($cacheItem->isHit()) {
235 $list[$id] = $cacheItem->get();
236 $loaded[$id] = $list[$id];
237 } else {
238 $cacheMisses[] = $id;
239 $list[$id] = $cacheItem;
240 }
241 }
242
243 // No cache pool misses, cache loaded items in-memory and return
244 if (empty($cacheMisses)) {
245 $this->logger->logCacheHit($arguments ?: $ids, 3);
246 $this->inMemory->setMulti($loaded, $cacheIndexes);
247
248 return $list;
249 }
250
251 // Load missing items, save to cache & apply to list if found
252 $backendLoadedList = $backendLoader($cacheMisses);
253 foreach ($cacheMisses as $id) {
254 if (isset($backendLoadedList[$id])) {
255 $this->cache->save(
256 $list[$id]
257 ->set($backendLoadedList[$id])
258 ->tag($cacheTagger($backendLoadedList[$id]))
259 );
260 $loaded[$id] = $backendLoadedList[$id];
261 $list[$id] = $backendLoadedList[$id];
262 } else {
263 // not found
264 unset($list[$id]);
265 }
266 }
267
268 // Save cache & log miss (this method expects multi backend loader to return empty array over throwing NotFound)
269 $this->inMemory->setMulti($loaded, $cacheIndexes);
270 unset($loaded, $backendLoadedList);
271 $this->logger->logCacheMiss($arguments ?: $cacheMisses, 3);
272
273 return $list;
274 }
275 }
eZ\Publish\Core\Repository\Values\$id
$id
Definition: GeneratorProxyTrait.php:32
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler
Abstract handler for use in other SPI Handlers.
Definition: AbstractInMemoryHandler.php:19
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler\getListCacheValue
getListCacheValue(string $key, callable $backendLoader, callable $cacheTagger, callable $cacheIndexes, callable $listTags=null, array $arguments=[])
Load list of objects of some type and loggs the hits / misses.
Definition: AbstractInMemoryHandler.php:127
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler\$cache
$cache
Definition: AbstractInMemoryHandler.php:25
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler\getMultipleCacheValues
getMultipleCacheValues(array $ids, string $keyPrefix, callable $backendLoader, callable $cacheTagger, callable $cacheIndexes, string $keySuffix='', array $arguments=[])
Load several cache items from cache and loggs the hits / misses.
Definition: AbstractInMemoryHandler.php:195
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler\$logger
$logger
Definition: AbstractInMemoryHandler.php:28
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler\$inMemory
$inMemory
Definition: AbstractInMemoryHandler.php:36
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler\__construct
__construct(TransactionAwareAdapterInterface $cache, PersistenceLogger $logger, InMemoryCache $inMemory)
Setups current handler with everything needed.
Definition: AbstractInMemoryHandler.php:45
eZ\Publish\Core\Persistence\Cache\AbstractInMemoryHandler\getCacheValue
getCacheValue( $id, string $keyPrefix, callable $backendLoader, callable $cacheTagger, callable $cacheIndexes, string $keySuffix='', array $arguments=[])
Load one cache item from cache and loggs the hits / misses.
Definition: AbstractInMemoryHandler.php:71
eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache
Simple internal In-Memory Cache Pool.
Definition: InMemoryCache.php:28
eZ\Publish\Core\Persistence\Cache\PersistenceLogger
Log un-cached & cached use of SPI Persistence.
Definition: PersistenceLogger.php:15
eZ\Publish\Core\Persistence\Cache\Adapter\TransactionAwareAdapterInterface
Interface for cache adapter which is aware of persistence transactions.
Definition: TransactionAwareAdapterInterface.php:22
eZ\Publish\Core\Persistence\Cache
Definition: AbstractHandler.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.