Skip to content

Commit

Permalink
Added getListCacheValue for loadRelationList
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszdebinski committed Apr 22, 2024
1 parent 23b33e2 commit b53d514
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 66 deletions.
105 changes: 54 additions & 51 deletions src/lib/Persistence/Cache/ContentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/lib/Resources/settings/storage_engines/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
18 changes: 9 additions & 9 deletions tests/lib/Persistence/Cache/ContentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit b53d514

Please sign in to comment.