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

MemoryCachingHandler.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\Type;
8
9 use eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache;
10 use eZ\Publish\SPI\Persistence\Content\Type;
11 use eZ\Publish\SPI\Persistence\Content\Type\Handler as BaseContentTypeHandler;
12 use eZ\Publish\SPI\Persistence\Content\Type\CreateStruct;
13 use eZ\Publish\SPI\Persistence\Content\Type\UpdateStruct;
14 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
15 use eZ\Publish\SPI\Persistence\Content\Type\Group;
16 use eZ\Publish\SPI\Persistence\Content\Type\Group\CreateStruct as GroupCreateStruct;
17 use eZ\Publish\SPI\Persistence\Content\Type\Group\UpdateStruct as GroupUpdateStruct;
18
19 class MemoryCachingHandler implements BaseContentTypeHandler
20 {
26 protected $innerHandler;
27
33 protected $cache;
34
41 public function __construct(BaseContentTypeHandler $handler, InMemoryCache $cache)
42 {
43 $this->innerHandler = $handler;
44 $this->cache = $cache;
45 }
46
50 public function createGroup(GroupCreateStruct $createStruct)
51 {
52 $group = $this->innerHandler->createGroup($createStruct);
53 $this->storeGroupCache([$group]);
54 $this->cache->deleteMulti(['ez-content-type-group-list']);
55
56 return $group;
57 }
58
62 public function updateGroup(GroupUpdateStruct $struct)
63 {
64 $group = $this->innerHandler->updateGroup($struct);
65 $this->storeGroupCache([$group]);
66 $this->cache->deleteMulti(['ez-content-type-group-list']);
67
68 return $group;
69 }
70
74 public function deleteGroup($groupId)
75 {
76 $this->innerHandler->deleteGroup($groupId);
77 // Delete by primary key will remove the object, so we don't need to clear `-by-identifier` variant here.
78 $this->cache->deleteMulti(['ez-content-type-group-' . $groupId, 'ez-content-type-group-list']);
79 }
80
84 public function loadGroup($groupId)
85 {
86 $group = $this->cache->get('ez-content-type-group-' . $groupId);
87 if ($group === null) {
88 $group = $this->innerHandler->loadGroup($groupId);
89 $this->storeGroupCache([$group]);
90 }
91
92 return $group;
93 }
94
98 public function loadGroups(array $groupIds)
99 {
100 $groups = $missingIds = [];
101 foreach ($groupIds as $groupId) {
102 if ($group = $this->cache->get('ez-content-type-group-' . $groupId)) {
103 $groups[$groupId] = $group;
104 } else {
105 $missingIds[] = $groupId;
106 }
107 }
108
109 if (!empty($missingIds)) {
110 $loaded = $this->innerHandler->loadGroups($missingIds);
111 $this->storeGroupCache($loaded);
113 $groups += $loaded;
114 }
115
116 return $groups;
117 }
118
122 public function loadGroupByIdentifier($identifier)
123 {
124 $group = $this->cache->get('ez-content-type-group-' . $identifier . '-by-identifier');
125 if ($group === null) {
126 $group = $this->innerHandler->loadGroupByIdentifier($identifier);
127 $this->storeGroupCache([$group]);
128 }
129
130 return $group;
131 }
132
136 public function loadAllGroups()
137 {
138 $groups = $this->cache->get('ez-content-type-group-list');
139 if ($groups === null) {
140 $groups = $this->innerHandler->loadAllGroups();
141 $this->storeGroupCache($groups, 'ez-content-type-group-list');
142 }
143
144 return $groups;
145 }
146
150 public function loadContentTypes($groupId, $status = Type::STATUS_DEFINED)
151 {
152 if ($status !== Type::STATUS_DEFINED) {
153 return $this->innerHandler->loadContentTypes($groupId, $status);
154 }
155
156 $types = $this->cache->get('ez-content-type-list-by-group-' . $groupId);
157 if ($types === null) {
158 $types = $this->innerHandler->loadContentTypes($groupId, $status);
159 $this->storeTypeCache($types, 'ez-content-type-list-by-group-' . $groupId);
160 }
161
162 return $types;
163 }
164
168 public function loadContentTypeList(array $contentTypeIds): array
169 {
170 $contentTypes = $missingIds = [];
171 foreach ($contentTypeIds as $contentTypeId) {
172 if ($contentType = $this->cache->get('ez-content-type-' . $contentTypeId . '-' . Type::STATUS_DEFINED)) {
173 $contentTypes[$contentTypeId] = $contentType;
174 } else {
175 $missingIds[] = $contentTypeId;
176 }
177 }
178
179 if (!empty($missingIds)) {
180 $loaded = $this->innerHandler->loadContentTypeList($missingIds);
181 $this->storeTypeCache($loaded);
183 $contentTypes += $loaded;
184 }
185
186 return $contentTypes;
187 }
188
192 public function load($contentTypeId, $status = Type::STATUS_DEFINED)
193 {
194 $contentType = $this->cache->get('ez-content-type-' . $contentTypeId . '-' . $status);
195 if ($contentType === null) {
196 $contentType = $this->innerHandler->load($contentTypeId, $status);
197 $this->storeTypeCache([$contentType]);
198 }
199
200 return $contentType;
201 }
202
206 public function loadByIdentifier($identifier)
207 {
208 $contentType = $this->cache->get('ez-content-type-' . $identifier . '-by-identifier');
209 if ($contentType === null) {
210 $contentType = $this->innerHandler->loadByIdentifier($identifier);
211 $this->storeTypeCache([$contentType]);
212 }
213
214 return $contentType;
215 }
216
220 public function loadByRemoteId($remoteId)
221 {
222 $contentType = $this->cache->get('ez-content-type-' . $remoteId . '-by-remote');
223 if ($contentType === null) {
224 $contentType = $this->innerHandler->loadByRemoteId($remoteId);
225 $this->storeTypeCache([$contentType]);
226 }
227
228 return $contentType;
229 }
230
234 public function create(CreateStruct $createStruct)
235 {
236 $contentType = $this->innerHandler->create($createStruct);
237 // Don't store as FieldTypeConstraints is not setup fully here from Legacy SE side
238 $this->deleteTypeCache($contentType->id, $contentType->status);
239
240 return $contentType;
241 }
242
246 public function update($typeId, $status, UpdateStruct $contentType)
247 {
248 $contentType = $this->innerHandler->update($typeId, $status, $contentType);
249 $this->storeTypeCache([$contentType]);
250
251 return $contentType;
252 }
253
257 public function delete($contentTypeId, $status)
258 {
259 $this->innerHandler->delete($contentTypeId, $status);
260 $this->deleteTypeCache($contentTypeId, $status);
261 }
262
266 public function createDraft($modifierId, $contentTypeId)
267 {
268 $contentType = $this->innerHandler->createDraft($modifierId, $contentTypeId);
269 // Don't store as FieldTypeConstraints is not setup fully here from Legacy SE side
270 $this->deleteTypeCache($contentType->id, $contentType->status);
271
272 return $contentType;
273 }
274
278 public function copy($userId, $contentTypeId, $status)
279 {
280 $contentType = $this->innerHandler->copy($userId, $contentTypeId, $status);
281 $this->storeTypeCache([$contentType]);
282
283 return $contentType;
284 }
285
289 public function unlink($groupId, $contentTypeId, $status)
290 {
291 $keys = ['ez-content-type-' . $contentTypeId . '-' . $status];
292 if ($status === Type::STATUS_DEFINED) {
293 $keys[] = 'ez-content-type-list-by-group-' . $groupId;
294 }
295 $this->cache->deleteMulti($keys);
296
297 return $this->innerHandler->unlink($groupId, $contentTypeId, $status);
298 }
299
303 public function link($groupId, $contentTypeId, $status)
304 {
305 $keys = ['ez-content-type-' . $contentTypeId . '-' . $status];
306 if ($status === Type::STATUS_DEFINED) {
307 $keys[] = 'ez-content-type-list-by-group-' . $groupId;
308 }
309 $this->cache->deleteMulti($keys);
310
311 return $this->innerHandler->link($groupId, $contentTypeId, $status);
312 }
313
317 public function getFieldDefinition($id, $status)
318 {
319 return $this->innerHandler->getFieldDefinition($id, $status);
320 }
321
325 public function getContentCount($contentTypeId)
326 {
327 return $this->innerHandler->getContentCount($contentTypeId);
328 }
329
333 public function addFieldDefinition($contentTypeId, $status, FieldDefinition $fieldDefinition)
334 {
335 $this->deleteTypeCache($contentTypeId, $status);
336
337 return $this->innerHandler->addFieldDefinition($contentTypeId, $status, $fieldDefinition);
338 }
339
343 public function removeFieldDefinition($contentTypeId, $status, $fieldDefinitionId)
344 {
345 $this->deleteTypeCache($contentTypeId, $status);
346
347 return $this->innerHandler->removeFieldDefinition($contentTypeId, $status, $fieldDefinitionId);
348 }
349
353 public function updateFieldDefinition($contentTypeId, $status, FieldDefinition $fieldDefinition)
354 {
355 $this->deleteTypeCache($contentTypeId, $status);
356
357 return $this->innerHandler->updateFieldDefinition($contentTypeId, $status, $fieldDefinition);
358 }
359
363 public function publish($contentTypeId)
364 {
365 $this->clearCache();
366
367 return $this->innerHandler->publish($contentTypeId);
368 }
369
373 public function getSearchableFieldMap()
374 {
375 $map = $this->cache->get('ez-content-type-field-map');
376 if ($map === null) {
377 $map = $this->innerHandler->getSearchableFieldMap();
378 $this->cache->setMulti(
379 $map,
380 static function () { return []; },
381 'ez-content-type-field-map'
382 );
383 }
384
385 return $map;
386 }
387
391 public function removeContentTypeTranslation(int $contentTypeId, string $languageCode): Type
392 {
393 $this->clearCache();
394
395 return $this->innerHandler->removeContentTypeTranslation($contentTypeId, $languageCode);
396 }
397
401 public function clearCache()
402 {
403 $this->cache->clear();
404 }
405
406 protected function deleteTypeCache(int $contentTypeId, int $status = Type::STATUS_DEFINED): void
407 {
408 if ($status !== Type::STATUS_DEFINED) {
409 // Delete by primary key will remove the object, so we don't need to clear other variants here.
410 $this->cache->deleteMulti(['ez-content-type-' . $contentTypeId . '-' . $status, 'ez-content-type-field-map']);
411 } else {
412 // We don't know group id in order to clear relevant "ez-content-type-list-by-group-$groupId".
413 $this->cache->clear();
414 }
415 }
416
417 protected function storeTypeCache(array $types, string $listIndex = null): void
418 {
419 $this->cache->setMulti(
420 $types,
421 static function (Type $type) {
422 if ($type->status !== Type::STATUS_DEFINED) {
423 return ['ez-content-type-' . $type->id . '-' . $type->status];
424 }
425
426 return [
427 'ez-content-type-' . $type->id . '-' . $type->status,
428 'ez-content-type-' . $type->identifier . '-by-identifier',
429 'ez-content-type-' . $type->remoteId . '-by-remote',
430 ];
431 },
432 $listIndex
433 );
434
435 $this->cache->deleteMulti(['ez-content-type-field-map']);
436 }
437
438 protected function storeGroupCache(array $groups, string $listIndex = null): void
439 {
440 $this->cache->setMulti(
441 $groups,
442 static function (Group $group) {
443 return [
444 'ez-content-type-group-' . $group->id,
445 'ez-content-type-group-' . $group->identifier . '-by-identifier',
446 ];
447 },
448 $listIndex
449 );
450 }
451
452 public function deleteByUserAndStatus(int $userId, int $status): void
453 {
454 $this->innerHandler->deleteByUserAndStatus($userId, $status);
455 }
456 }
eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache
Simple internal In-Memory Cache Pool.
Definition: InMemoryCache.php:28
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler
Definition: MemoryCachingHandler.php:20
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\clearCache
clearCache()
Clear internal caches.
Definition: MemoryCachingHandler.php:401
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\$cache
$cache
Definition: MemoryCachingHandler.php:33
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\create
create(CreateStruct $createStruct)
{}
Definition: MemoryCachingHandler.php:234
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\publish
publish($contentTypeId)
{Update content objects.Updates content objects, depending on the changed field definitions....
Definition: MemoryCachingHandler.php:363
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\$innerHandler
$innerHandler
Definition: MemoryCachingHandler.php:26
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\storeGroupCache
storeGroupCache(array $groups, string $listIndex=null)
Definition: MemoryCachingHandler.php:438
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\update
update($typeId, $status, UpdateStruct $contentType)
{}
Definition: MemoryCachingHandler.php:246
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\copy
copy($userId, $contentTypeId, $status)
{Copy a Type incl fields and group-relations from a given status to a new Type with status Type::STAT...
Definition: MemoryCachingHandler.php:278
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\getSearchableFieldMap
getSearchableFieldMap()
{Returns content type, field definition and field type mapping information for search engine usage....
Definition: MemoryCachingHandler.php:373
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\storeTypeCache
storeTypeCache(array $types, string $listIndex=null)
Definition: MemoryCachingHandler.php:417
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\deleteGroup
deleteGroup($groupId)
{}
Definition: MemoryCachingHandler.php:74
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\unlink
unlink($groupId, $contentTypeId, $status)
{Unlink a content type group from a content type.One of Type::STATUS_DEFINED|Type::STATUS_DRAFT|Type:...
Definition: MemoryCachingHandler.php:289
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\getContentCount
getContentCount($contentTypeId)
{Counts the number of Content instances of the ContentType identified by given $contentTypeId....
Definition: MemoryCachingHandler.php:325
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\addFieldDefinition
addFieldDefinition($contentTypeId, $status, FieldDefinition $fieldDefinition)
{Adds a new field definition to an existing Type.This method creates a new version of the Type with t...
Definition: MemoryCachingHandler.php:333
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\createGroup
createGroup(GroupCreateStruct $createStruct)
{\eZ\Publish\SPI\Persistence\Content\Type\Group}
Definition: MemoryCachingHandler.php:50
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\deleteTypeCache
deleteTypeCache(int $contentTypeId, int $status=Type::STATUS_DEFINED)
Definition: MemoryCachingHandler.php:406
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\updateGroup
updateGroup(GroupUpdateStruct $struct)
{}
Definition: MemoryCachingHandler.php:62
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadByIdentifier
loadByIdentifier($identifier)
{Loads a (defined) content type by identifier.Note: This method is responsible of having the Field De...
Definition: MemoryCachingHandler.php:206
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\removeFieldDefinition
removeFieldDefinition($contentTypeId, $status, $fieldDefinitionId)
{Removes a field definition from an existing Type.This method creates a new version of the Type with ...
Definition: MemoryCachingHandler.php:343
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\link
link($groupId, $contentTypeId, $status)
{Link a content type group with a content type.One of Type::STATUS_DEFINED|Type::STATUS_DRAFT|Type::S...
Definition: MemoryCachingHandler.php:303
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\deleteByUserAndStatus
deleteByUserAndStatus(int $userId, int $status)
Definition: MemoryCachingHandler.php:452
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\removeContentTypeTranslation
removeContentTypeTranslation(int $contentTypeId, string $languageCode)
{\eZ\Publish\SPI\Persistence\Content\Type}
Definition: MemoryCachingHandler.php:391
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadGroupByIdentifier
loadGroupByIdentifier($identifier)
{Loads Type Group by identifier.Legacy note: Uses name for identifier.}
Definition: MemoryCachingHandler.php:122
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\getFieldDefinition
getFieldDefinition($id, $status)
{Returns field definition for the given field definition id.}
Definition: MemoryCachingHandler.php:317
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadContentTypes
loadContentTypes($groupId, $status=Type::STATUS_DEFINED)
{One of Type::STATUS_DEFINED|Type::STATUS_DRAFT|Type::STATUS_MODIFIED\eZ\Publish\SPI\Persistence\Cont...
Definition: MemoryCachingHandler.php:150
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadContentTypeList
loadContentTypeList(array $contentTypeIds)
{Return list of unique Content Types, with type id as key.Missing items (NotFound) will be missing fr...
Definition: MemoryCachingHandler.php:168
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadAllGroups
loadAllGroups()
{\eZ\Publish\SPI\Persistence\Content\Type\Group[]}
Definition: MemoryCachingHandler.php:136
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\__construct
__construct(BaseContentTypeHandler $handler, InMemoryCache $cache)
Creates a new content type handler.
Definition: MemoryCachingHandler.php:41
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadGroup
loadGroup($groupId)
{}
Definition: MemoryCachingHandler.php:84
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadGroups
loadGroups(array $groupIds)
{Return list of unique Content Type Groups, with group id as key.Missing items (NotFound) will be mis...
Definition: MemoryCachingHandler.php:98
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\load
load($contentTypeId, $status=Type::STATUS_DEFINED)
{Loads a content type by id and status.Note: This method is responsible of having the Field Definitio...
Definition: MemoryCachingHandler.php:192
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\createDraft
createDraft($modifierId, $contentTypeId)
{Creates a draft of existing defined content type.Updates modified date, sets $modifierId and status ...
Definition: MemoryCachingHandler.php:266
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\updateFieldDefinition
updateFieldDefinition($contentTypeId, $status, FieldDefinition $fieldDefinition)
{This method updates the given $fieldDefinition on a Type.This method creates a new version of the Ty...
Definition: MemoryCachingHandler.php:353
eZ\Publish\Core\Persistence\Legacy\Content\Type\MemoryCachingHandler\loadByRemoteId
loadByRemoteId($remoteId)
{Loads a (defined) content type by remote id.Note: This method is responsible of having the Field Def...
Definition: MemoryCachingHandler.php:220
eZ\Publish\SPI\Persistence\Content\Type\CreateStruct
Definition: SPI/Persistence/Content/Type/CreateStruct.php:13
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
eZ\Publish\SPI\Persistence\Content\Type\Group\CreateStruct
Definition: SPI/Persistence/Content/Type/Group/CreateStruct.php:12
eZ\Publish\SPI\Persistence\Content\Type\Group\UpdateStruct
Definition: Content/Type/Group/UpdateStruct.php:12
eZ\Publish\SPI\Persistence\Content\Type\Group
Definition: Type/Group.php:12
eZ\Publish\SPI\Persistence\Content\Type\UpdateStruct
Definition: Content/Type/UpdateStruct.php:12
eZ\Publish\SPI\Persistence\Content\Type
SPI Persistence Content\Type value object.
Definition: SPI/Persistence/Content/Type.php:15
eZ\Publish\SPI\Persistence\Content\Type\STATUS_DEFINED
const STATUS_DEFINED
Definition: SPI/Persistence/Content/Type.php:17
eZ\Publish\SPI\Persistence\Content\Type\$identifier
$identifier
Definition: SPI/Persistence/Content/Type.php:68
eZ\Publish\SPI\Persistence\Content\Type\$remoteId
$remoteId
Definition: SPI/Persistence/Content/Type.php:103
eZ\Publish\SPI\Persistence\Content\Type\$id
$id
Definition: SPI/Persistence/Content/Type.php:30
eZ\Publish\SPI\Persistence\Content\Type\$modifierId
$modifierId
Definition: SPI/Persistence/Content/Type.php:96
eZ\Publish\SPI\Persistence\Content\Type\$groupIds
$groupIds
Definition: SPI/Persistence/Content/Type.php:165
eZ\Publish\SPI\Persistence\Content\Type\$status
$status
Definition: SPI/Persistence/Content/Type.php:37
eZ\Publish\SPI\Persistence\Content\Type\Handler
Definition: SPI/Persistence/Content/Type/Handler.php:14
eZ\Publish\SPI\Persistence\Content\Type\Group
Definition: SPI/Persistence/Content/Type/Group/CreateStruct.php:7
eZ\Publish\SPI\Persistence\Content\Type
Definition: SPI/Persistence/Content/Type/CreateStruct.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.