Skip to content

Commit 309b952

Browse files
Added cache for RelationList, Added deprecated and @throws for loadRelations in contracts
1 parent 558086b commit 309b952

File tree

8 files changed

+84
-21
lines changed

8 files changed

+84
-21
lines changed

src/contracts/Persistence/Content/Handler.php

+2
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ public function removeRelation($relationId, $type, ?int $destinationContentId =
283283
/**
284284
* Loads relations from $sourceContentId. Optionally, loads only those with $type and $sourceContentVersionNo.
285285
*
286+
* @deprecated since 4.5, use loadRelationList() instead.
287+
*
286288
* @param mixed $sourceContentId Source Content ID
287289
* @param mixed|null $sourceContentVersionNo Source Content Version, null if not specified
288290
* @param int|null $type {@see \Ibexa\Contracts\Core\Repository\Values\Content\Relation::COMMON,

src/contracts/Repository/ContentService.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $dest
400400
*
401401
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version
402402
*
403-
* @deprecated since 4.5, use loadRelationList().
403+
* @deprecated since 4.5, use loadRelationList() instead.
404404
*
405405
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Relation[]
406406
*/
@@ -411,6 +411,9 @@ public function loadRelations(VersionInfo $versionInfo): iterable;
411411
*
412412
* If the user is not allowed to read specific version then UnauthorizedRelationListItem is returned
413413
*
414+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
415+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
416+
*
414417
* @see \Ibexa\Contracts\Core\Repository\Values\Content\RelationList\Item\UnauthorizedRelationListItem
415418
*/
416419
public function loadRelationList(
@@ -421,6 +424,9 @@ public function loadRelationList(
421424

422425
/**
423426
* Counts all outgoing relations for the given version.
427+
*
428+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
429+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
424430
*/
425431
public function countRelations(VersionInfo $versionInfo): int;
426432

src/lib/Persistence/Cache/ContentHandler.php

+55-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class ContentHandler extends AbstractInMemoryPersistenceHandler implements Conte
3535
private const CONTENT_RELATIONS_COUNT_WITH_TYPE_IDENTIFIER = 'content_relations_count_with_type';
3636
private const CONTENT_RELATIONS_COUNT_WITH_TYPE_AND_VERSION_IDENTIFIER = 'content_relations_count_with_type_and_version';
3737
private const CONTENT_RELATIONS_COUNT_WITH_VERSION_IDENTIFIER = 'content_relations_count_with_version';
38+
private const CONTENT_RELATIONS_LIST_IDENTIFIER = 'content_relations_list';
39+
private const CONTENT_RELATIONS_LIST_WITH_TYPE_IDENTIFIER = 'content_relations_list_with_type';
40+
private const CONTENT_RELATIONS_LIST_WITH_TYPE_AND_VERSION_IDENTIFIER = 'content_relations_list_with_type_and_version';
41+
private const CONTENT_RELATIONS_LIST_WITH_VERSION_IDENTIFIER = 'content_relations_list_with_version';
3842
private const CONTENT_REVERSE_RELATIONS_COUNT_IDENTIFIER = 'content_reverse_relations_count';
3943
private const RELATION_IDENTIFIER = 'relation';
4044

@@ -546,12 +550,12 @@ public function countRelations(int $sourceContentId, ?int $sourceContentVersionN
546550
);
547551

548552
if ($cacheItem->isHit()) {
549-
$this->logger->logCacheHit(['content' => $sourceContentId, 'version' => $sourceContentVersionNo]);
553+
$this->logger->logCacheHit(['content' => $sourceContentId, 'version' => $sourceContentVersionNo, 'type' => $type]);
550554

551555
return $cacheItem->get();
552556
}
553557

554-
$this->logger->logCacheMiss(['content' => $sourceContentId, 'version' => $sourceContentVersionNo]);
558+
$this->logger->logCacheMiss(['content' => $sourceContentId, 'version' => $sourceContentVersionNo, 'type' => $type]);
555559
$relationsCount = $this->persistenceHandler->contentHandler()->countRelations(
556560
$sourceContentId,
557561
$sourceContentVersionNo,
@@ -571,31 +575,72 @@ public function countRelations(int $sourceContentId, ?int $sourceContentVersionN
571575
return $relationsCount;
572576
}
573577

574-
/**
575-
* {@inheritdoc}
576-
*/
577578
public function loadRelationList(
578579
int $sourceContentId,
579580
int $limit,
580581
int $offset = 0,
581582
?int $sourceContentVersionNo = null,
582583
?int $type = null
583584
): array {
584-
$this->logger->logCall(__METHOD__, [
585+
$values = [$sourceContentId, $limit, $offset];
586+
587+
if ($sourceContentVersionNo === null && $type !== null) {
588+
$patternName = self::CONTENT_RELATIONS_LIST_WITH_TYPE_IDENTIFIER;
589+
$values[] = $type;
590+
} elseif ($sourceContentVersionNo !== null && $type === null) {
591+
$patternName = self::CONTENT_RELATIONS_LIST_WITH_VERSION_IDENTIFIER;
592+
$values[] = $sourceContentVersionNo;
593+
} elseif ($sourceContentVersionNo !== null && $type !== null) {
594+
$patternName = self::CONTENT_RELATIONS_LIST_WITH_TYPE_AND_VERSION_IDENTIFIER;
595+
$values = array_merge($values, [$type, $sourceContentVersionNo]);
596+
} else {
597+
$patternName = self::CONTENT_RELATIONS_LIST_IDENTIFIER;
598+
}
599+
600+
$cacheItem = $this->cache->getItem(
601+
$this->cacheIdentifierGenerator->generateKey(
602+
$patternName,
603+
$values,
604+
true
605+
)
606+
);
607+
608+
$logCacheArguments = [
585609
'content' => $sourceContentId,
586610
'version' => $sourceContentVersionNo,
587-
'offset' => $offset,
588-
'limit' => $limit,
589611
'type' => $type,
590-
]);
612+
'limit' => $limit,
613+
'offset' => $offset,
614+
];
615+
616+
if ($cacheItem->isHit()) {
617+
$this->logger->logCacheHit($logCacheArguments);
618+
619+
return $cacheItem->get();
620+
}
621+
622+
$this->logger->logCacheMiss($logCacheArguments);
591623

592-
return $this->persistenceHandler->contentHandler()->loadRelationList(
624+
$relationList = $this->persistenceHandler->contentHandler()->loadRelationList(
593625
$sourceContentId,
594626
$limit,
595627
$offset,
596628
$sourceContentVersionNo,
597629
$type
598630
);
631+
632+
$cacheItem->set($relationList);
633+
$tags = [
634+
$this->cacheIdentifierGenerator->generateTag(
635+
self::CONTENT_IDENTIFIER,
636+
[$sourceContentId]
637+
),
638+
];
639+
640+
$cacheItem->tag($tags);
641+
$this->cache->save($cacheItem);
642+
643+
return $relationList;
599644
}
600645

601646
/**

src/lib/Persistence/Legacy/Content/Gateway/DoctrineDatabase.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1435,10 +1435,10 @@ private function prepareRelationQuery(
14351435
->innerJoin(
14361436
'l',
14371437
self::CONTENT_ITEM_TABLE,
1438-
'ezcontentobject_to',
1438+
'c_to',
14391439
$expr->and(
1440-
'l.to_contentobject_id = ezcontentobject_to.id',
1441-
'ezcontentobject_to.status = :status'
1440+
'l.to_contentobject_id = c_to.id',
1441+
'c_to.status = :status'
14421442
)
14431443
)
14441444
->where(
@@ -1460,7 +1460,7 @@ private function prepareRelationQuery(
14601460
// from published version only
14611461
$query
14621462
->innerJoin(
1463-
'ezcontentobject_to',
1463+
'c_to',
14641464
self::CONTENT_ITEM_TABLE,
14651465
'c',
14661466
$expr->and(

src/lib/Persistence/Legacy/Content/Gateway/ExceptionConversion.php

-3
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,6 @@ public function countRelations(
404404
}
405405
}
406406

407-
/**
408-
* {@inheritdoc}
409-
*/
410407
public function listRelations(
411408
int $contentId,
412409
int $limit,

src/lib/Persistence/Legacy/Content/Handler.php

-3
Original file line numberDiff line numberDiff line change
@@ -827,9 +827,6 @@ public function countRelations(int $sourceContentId, ?int $sourceContentVersionN
827827
return $this->contentGateway->countRelations($sourceContentId, $sourceContentVersionNo, $type);
828828
}
829829

830-
/**
831-
* {@inheritdoc}
832-
*/
833830
public function loadRelationList(
834831
int $sourceContentId,
835832
int $limit,

src/lib/Resources/settings/storage_engines/cache.yml

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ parameters:
121121
content_relations_count_with_type: 'crc-%%s-t-%%s'
122122
content_relations_count_with_type_and_version: 'crc-%%s-t-%%s-v-%%s'
123123
content_relations_count_with_version: 'crc-%%s-v-%%s'
124+
content_relations_list: 'crl-%%s-l-%%s-o-%%s'
125+
content_relations_list_with_type: 'crl-%%s-l-%%s-o-%%s-t-%%s'
126+
content_relations_list_with_type_and_version: 'crl-%%s-l-%%s-o-%%s-t-%%s-v-%%s'
127+
content_relations_list_with_version: 'crl-%%s-l-%%s-o-%%s-v-%%s'
124128
content_reverse_relations_count: 'crrc-%s'
125129
content_version_info: 'cvi-%s'
126130
content_version_list: 'c-%s-vl'

tests/lib/Persistence/Cache/ContentHandlerTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Ibexa\Contracts\Core\Persistence\Content\CreateStruct;
1212
use Ibexa\Contracts\Core\Persistence\Content\Handler as SPIContentHandler;
1313
use Ibexa\Contracts\Core\Persistence\Content\MetadataUpdateStruct;
14+
use Ibexa\Contracts\Core\Persistence\Content\Relation;
1415
use Ibexa\Contracts\Core\Persistence\Content\Relation as SPIRelation;
1516
use Ibexa\Contracts\Core\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
1617
use Ibexa\Contracts\Core\Persistence\Content\UpdateStruct;
@@ -82,6 +83,13 @@ public function providerForCachedLoadMethodsHit(): array
8283
$info = new ContentInfo(['id' => 2]);
8384
$version = new VersionInfo(['versionNo' => 1, 'contentInfo' => $info]);
8485
$content = new Content(['fields' => [], 'versionInfo' => $version]);
86+
$relation = new Relation();
87+
$relation->id = 1;
88+
$relation->sourceContentId = 2;
89+
$relation->sourceContentVersionNo = 2;
90+
$relation->destinationContentId = 1;
91+
$relation->type = 1;
92+
$relationList[1] = $relation;
8593

8694
// string $method, array $arguments, string $key, array? $tagGeneratingArguments, array? $tagGeneratingResults, array? $keyGeneratingArguments, array? $keyGeneratingResults, mixed? $data, bool $multi = false, array $additionalCalls
8795
return [
@@ -90,6 +98,10 @@ public function providerForCachedLoadMethodsHit(): array
9098
['countRelations', [2, 2], 'ibx-crc-2-v-2', null, null, [['content_relations_count_with_version', [2, 2], true]], ['ibx-crc-2-v-2'], 10],
9199
['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],
92100
['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],
101+
['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],
102+
['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],
103+
['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],
104+
['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],
93105
['load', [2, 1], 'ibx-c-2-1-' . ContentHandler::ALL_TRANSLATIONS_KEY, null, null, [['content', [], true]], ['ibx-c'], $content],
94106
['load', [2, 1, ['eng-GB', 'eng-US']], 'ibx-c-2-1-eng-GB|eng-US', null, null, [['content', [], true]], ['ibx-c'], $content],
95107
['load', [2], 'ibx-c-2-' . ContentHandler::ALL_TRANSLATIONS_KEY, null, null, [['content', [], true]], ['ibx-c'], $content],

0 commit comments

Comments
 (0)