From 9c17daa08edfdca5e8442a964adc0cdff1fc428e Mon Sep 17 00:00:00 2001 From: matx132 Date: Wed, 27 Mar 2024 11:09:38 +0100 Subject: [PATCH] Added getListCacheValue for loadRelationList --- src/lib/Persistence/Cache/ContentHandler.php | 105 +++++++++--------- .../settings/storage_engines/cache.yml | 14 ++- .../Persistence/Cache/ContentHandlerTest.php | 18 +-- 3 files changed, 71 insertions(+), 66 deletions(-) diff --git a/src/lib/Persistence/Cache/ContentHandler.php b/src/lib/Persistence/Cache/ContentHandler.php index b40fd8db3f..747dd5e3f0 100644 --- a/src/lib/Persistence/Cache/ContentHandler.php +++ b/src/lib/Persistence/Cache/ContentHandler.php @@ -32,13 +32,14 @@ class ContentHandler extends AbstractInMemoryPersistenceHandler implements Conte private const CONTENT_VERSION_INFO_IDENTIFIER = 'content_version_info'; private const CONTENT_VERSION_IDENTIFIER = 'content_version'; private const CONTENT_RELATIONS_COUNT_IDENTIFIER = 'content_relations_count'; - private const CONTENT_RELATIONS_COUNT_WITH_TYPE_IDENTIFIER = 'content_relations_count_with_type'; - private const CONTENT_RELATIONS_COUNT_WITH_TYPE_AND_VERSION_IDENTIFIER = 'content_relations_count_with_type_and_version'; - private const CONTENT_RELATIONS_COUNT_WITH_VERSION_IDENTIFIER = 'content_relations_count_with_version'; + private const CONTENT_RELATIONS_COUNT_WITH_BY_TYPE_IDENTIFIER = 'content_relations_count_with_by_type_suffix'; + private const CONTENT_RELATIONS_COUNT_WITH_BY_TYPE_VERSION_IDENTIFIER = 'content_relations_count_with_by_type_version_suffix'; + private const CONTENT_RELATIONS_COUNT_WITH_BY_VERSION_IDENTIFIER = 'content_relations_count_with_by_version_suffix'; + private const CONTENT_RELATION_IDENTIFIER = 'content_relation'; private const CONTENT_RELATIONS_LIST_IDENTIFIER = 'content_relations_list'; - private const CONTENT_RELATIONS_LIST_WITH_TYPE_IDENTIFIER = 'content_relations_list_with_type'; - private const CONTENT_RELATIONS_LIST_WITH_TYPE_AND_VERSION_IDENTIFIER = 'content_relations_list_with_type_and_version'; - private const CONTENT_RELATIONS_LIST_WITH_VERSION_IDENTIFIER = 'content_relations_list_with_version'; + private const CONTENT_RELATIONS_LIST_WITH_BY_TYPE_IDENTIFIER = 'content_relations_list_with_by_type_suffix'; + private const CONTENT_RELATIONS_LIST_WITH_BY_TYPE_VERSION_IDENTIFIER = 'content_relations_list_with_by_type_version_suffix'; + private const CONTENT_RELATIONS_LIST_WITH_BY_VERSION_IDENTIFIER = 'content_relations_list_with_by_version_suffix'; private const CONTENT_REVERSE_RELATIONS_COUNT_IDENTIFIER = 'content_reverse_relations_count'; private const RELATION_IDENTIFIER = 'relation'; @@ -529,13 +530,13 @@ public function countRelations(int $sourceContentId, ?int $sourceContentVersionN { $values[] = $sourceContentId; if ($sourceContentVersionNo === null && $type !== null) { - $patternName = self::CONTENT_RELATIONS_COUNT_WITH_TYPE_IDENTIFIER; + $patternName = self::CONTENT_RELATIONS_COUNT_WITH_BY_TYPE_IDENTIFIER; $values[] = $type; } elseif ($sourceContentVersionNo !== null && $type === null) { - $patternName = self::CONTENT_RELATIONS_COUNT_WITH_VERSION_IDENTIFIER; + $patternName = self::CONTENT_RELATIONS_COUNT_WITH_BY_VERSION_IDENTIFIER; $values[] = $sourceContentVersionNo; } elseif ($sourceContentVersionNo !== null && $type !== null) { - $patternName = self::CONTENT_RELATIONS_COUNT_WITH_TYPE_AND_VERSION_IDENTIFIER; + $patternName = self::CONTENT_RELATIONS_COUNT_WITH_BY_TYPE_VERSION_IDENTIFIER; $values = array_merge($values, [$type, $sourceContentVersionNo]); } else { $patternName = self::CONTENT_RELATIONS_COUNT_IDENTIFIER; @@ -585,62 +586,64 @@ public function loadRelationList( $values = [$sourceContentId, $limit, $offset]; if ($sourceContentVersionNo === null && $type !== null) { - $patternName = self::CONTENT_RELATIONS_LIST_WITH_TYPE_IDENTIFIER; + $patternName = self::CONTENT_RELATIONS_LIST_WITH_BY_TYPE_IDENTIFIER; $values[] = $type; } elseif ($sourceContentVersionNo !== null && $type === null) { - $patternName = self::CONTENT_RELATIONS_LIST_WITH_VERSION_IDENTIFIER; + $patternName = self::CONTENT_RELATIONS_LIST_WITH_BY_VERSION_IDENTIFIER; $values[] = $sourceContentVersionNo; } elseif ($sourceContentVersionNo !== null && $type !== null) { - $patternName = self::CONTENT_RELATIONS_LIST_WITH_TYPE_AND_VERSION_IDENTIFIER; + $patternName = self::CONTENT_RELATIONS_LIST_WITH_BY_TYPE_VERSION_IDENTIFIER; $values = array_merge($values, [$type, $sourceContentVersionNo]); } else { $patternName = self::CONTENT_RELATIONS_LIST_IDENTIFIER; } - $cacheItem = $this->cache->getItem( + return $this->getListCacheValue( $this->cacheIdentifierGenerator->generateKey( $patternName, $values, true - ) - ); - - $logCacheArguments = [ - 'content' => $sourceContentId, - 'version' => $sourceContentVersionNo, - 'type' => $type, - 'limit' => $limit, - 'offset' => $offset, - ]; - - if ($cacheItem->isHit()) { - $this->logger->logCacheHit($logCacheArguments); - - return $cacheItem->get(); - } - - $this->logger->logCacheMiss($logCacheArguments); - - $relationList = $this->persistenceHandler->contentHandler()->loadRelationList( - $sourceContentId, - $limit, - $offset, - $sourceContentVersionNo, - $type - ); - - $cacheItem->set($relationList); - $tags = [ - $this->cacheIdentifierGenerator->generateTag( - self::CONTENT_IDENTIFIER, - [$sourceContentId] ), - ]; - - $cacheItem->tag($tags); - $this->cache->save($cacheItem); - - return $relationList; + function () use ($sourceContentId, $limit, $offset, $sourceContentVersionNo, $type): array { + return $this->persistenceHandler->contentHandler()->loadRelationList( + $sourceContentId, + $limit, + $offset, + $sourceContentVersionNo, + $type + ); + }, + function (Relation $relation): array { + return [ + $this->cacheIdentifierGenerator->generateTag( + self::CONTENT_RELATION_IDENTIFIER, + [$relation->destinationContentId] + ), + $this->cacheIdentifierGenerator->generateTag( + self::CONTENT_IDENTIFIER, + [$relation->destinationContentId] + ), + ]; + }, + function (Relation $relation): array { + return [ + $this->cacheIdentifierGenerator->generateKey(self::CONTENT_IDENTIFIER, [$relation->destinationContentId], true), + ]; + }, + function () use ($sourceContentId): array { + return [ + $this->cacheIdentifierGenerator->generateTag( + self::CONTENT_RELATIONS_LIST_IDENTIFIER, + [$sourceContentId] + ), + $this->cacheIdentifierGenerator->generateTag( + self::CONTENT_IDENTIFIER, + [$sourceContentId] + ), + ]; + }, + $values + ); } /** diff --git a/src/lib/Resources/settings/storage_engines/cache.yml b/src/lib/Resources/settings/storage_engines/cache.yml index 3647a7f65a..072b1472d8 100644 --- a/src/lib/Resources/settings/storage_engines/cache.yml +++ b/src/lib/Resources/settings/storage_engines/cache.yml @@ -31,6 +31,8 @@ parameters: content_type_group_with_id_suffix: 'ctg-%s-bi' content_type_group_list: 'ctgl-%s' content_type_list_by_group: 'ctlbg-%s' + content_relation: 'cr-%s' + content_relations_list: 'crl-%s' image_variation: 'ig' image_variation_name: 'ign-%s' image_variation_siteaccess: 'igs-%s' @@ -118,13 +120,13 @@ parameters: content_info_by_remote_id: 'cibri-%s' content_locations: 'cl-%s' content_relations_count: 'crc-%s' - content_relations_count_with_type: 'crc-%%s-t-%%s' - content_relations_count_with_type_and_version: 'crc-%%s-t-%%s-v-%%s' - content_relations_count_with_version: 'crc-%%s-v-%%s' + content_relations_count_with_by_type_suffix: 'crc-%%s-t-%%s' + content_relations_count_with_by_type_version_suffix: 'crc-%%s-t-%%s-v-%%s' + content_relations_count_with_by_version_suffix: 'crc-%%s-v-%%s' content_relations_list: 'crl-%%s-l-%%s-o-%%s' - content_relations_list_with_type: 'crl-%%s-l-%%s-o-%%s-t-%%s' - content_relations_list_with_type_and_version: 'crl-%%s-l-%%s-o-%%s-t-%%s-v-%%s' - content_relations_list_with_version: 'crl-%%s-l-%%s-o-%%s-v-%%s' + content_relations_list_with_by_type_suffix: 'crl-%%s-l-%%s-o-%%s-t-%%s' + content_relations_list_with_by_type_version_suffix: 'crl-%%s-l-%%s-o-%%s-t-%%s-v-%%s' + content_relations_list_with_by_version_suffix: 'crl-%%s-l-%%s-o-%%s-v-%%s' content_reverse_relations_count: 'crrc-%s' content_version_info: 'cvi-%s' content_version_list: 'c-%s-vl' diff --git a/tests/lib/Persistence/Cache/ContentHandlerTest.php b/tests/lib/Persistence/Cache/ContentHandlerTest.php index 8206cbf0ee..171efa83b7 100644 --- a/tests/lib/Persistence/Cache/ContentHandlerTest.php +++ b/tests/lib/Persistence/Cache/ContentHandlerTest.php @@ -95,13 +95,13 @@ public function providerForCachedLoadMethodsHit(): array return [ ['countReverseRelations', [2], 'ibx-crrc-2', null, null, [['content_reverse_relations_count', [2], true]], ['ibx-crrc-2'], 10], ['countRelations', [2], 'ibx-crc-2', null, null, [['content_relations_count', [2], true]], ['ibx-crc-2'], 10], - ['countRelations', [2, 2], 'ibx-crc-2-v-2', null, null, [['content_relations_count_with_version', [2, 2], true]], ['ibx-crc-2-v-2'], 10], - ['countRelations', [2, null, 1], 'ibx-crc-2-t-1', null, null, [['content_relations_count_with_type', [2, 1], true]], ['ibx-crc-2-t-1'], 10], - ['countRelations', [2, 2, 1], 'ibx-crc-2-t-1-v-2', null, null, [['content_relations_count_with_type_and_version', [2, 1, 2], true]], ['ibx-crc-2-t-1-v-2'], 10], + ['countRelations', [2, 2], 'ibx-crc-2-v-2', null, null, [['content_relations_count_with_by_version_suffix', [2, 2], true]], ['ibx-crc-2-v-2'], 10], + ['countRelations', [2, null, 1], 'ibx-crc-2-t-1', null, null, [['content_relations_count_with_by_type_suffix', [2, 1], true]], ['ibx-crc-2-t-1'], 10], + ['countRelations', [2, 2, 1], 'ibx-crc-2-t-1-v-2', null, null, [['content_relations_count_with_by_type_version_suffix', [2, 1, 2], true]], ['ibx-crc-2-t-1-v-2'], 10], ['loadRelationList', [2, 1, 0], 'ibx-crl-2-l-1-o-0', null, null, [['content_relations_list', [2, 1, 0], true]], ['ibx-crl-2-l-1-o-0'], $relationList], - ['loadRelationList', [2, 1, 0, 2], 'ibx-crl-2-l-1-o-0-v-2', null, null, [['content_relations_list_with_version', [2, 1, 0, 2], true]], ['ibx-crl-2-l-1-o-0-v-2'], $relationList], - ['loadRelationList', [2, 1, 0, null, 1], 'ibx-crl-2-l-1-o-0-t-1', null, null, [['content_relations_list_with_type', [2, 1, 0, 1], true]], ['ibx-crl-2-l-1-o-0-t-1'], $relationList], - ['loadRelationList', [2, 1, 0, 2, 1], 'ibx-crl-2-l-1-o-0-t-1-v-2', null, null, [['content_relations_list_with_type_and_version', [2, 1, 0, 1, 2], true]], ['ibx-crl-2-l-1-o-0-t-1-v-2'], $relationList], + ['loadRelationList', [2, 1, 0, 2], 'ibx-crl-2-l-1-o-0-v-2', null, null, [['content_relations_list_with_by_version_suffix', [2, 1, 0, 2], true]], ['ibx-crl-2-l-1-o-0-v-2'], $relationList], + ['loadRelationList', [2, 1, 0, null, 1], 'ibx-crl-2-l-1-o-0-t-1', null, null, [['content_relations_list_with_by_type_suffix', [2, 1, 0, 1], true]], ['ibx-crl-2-l-1-o-0-t-1'], $relationList], + ['loadRelationList', [2, 1, 0, 2, 1], 'ibx-crl-2-l-1-o-0-t-1-v-2', null, null, [['content_relations_list_with_by_type_version_suffix', [2, 1, 0, 1, 2], true]], ['ibx-crl-2-l-1-o-0-t-1-v-2'], $relationList], ['load', [2, 1], 'ibx-c-2-1-' . ContentHandler::ALL_TRANSLATIONS_KEY, null, null, [['content', [], true]], ['ibx-c'], $content], ['load', [2, 1, ['eng-GB', 'eng-US']], 'ibx-c-2-1-eng-GB|eng-US', null, null, [['content', [], true]], ['ibx-c'], $content], ['load', [2], 'ibx-c-2-' . ContentHandler::ALL_TRANSLATIONS_KEY, null, null, [['content', [], true]], ['ibx-c'], $content], @@ -170,7 +170,7 @@ public function providerForCachedLoadMethodsMiss(): array ], ['c-2'], [ - ['content_relations_count_with_version', [2, 3], true], + ['content_relations_count_with_by_version_suffix', [2, 3], true], ], ['ibx-crc-2-v-3'], 10, @@ -184,7 +184,7 @@ public function providerForCachedLoadMethodsMiss(): array ], ['c-2'], [ - ['content_relations_count_with_type', [2, 1], true], + ['content_relations_count_with_by_type_suffix', [2, 1], true], ], ['ibx-crc-2-t-1'], 10, @@ -198,7 +198,7 @@ public function providerForCachedLoadMethodsMiss(): array ], ['c-2'], [ - ['content_relations_count_with_type_and_version', [2, 1, 3], true], + ['content_relations_count_with_by_type_version_suffix', [2, 1, 3], true], ], ['ibx-crc-2-t-1-v-3'], 10,