Skip to content

TASK: (Re-)introduce runtime cache on content repository #5505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 9.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Neos.ContentRepository.Core/Classes/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
*/
final class ContentRepository
{
/**
* @var array<string,ContentGraphInterface>|false indexed by workspace name, false if disabled
*/
private array|false $contentGraphsRuntimeCache = [];

/** @var Workspaces|false|null false if disabled */
private Workspaces|null|false $workspacesRuntimeCache = null;

/**
* @internal use the {@see ContentRepositoryFactory::getOrBuild()} to instantiate
*/
Expand Down Expand Up @@ -95,6 +103,7 @@ public function handle(CommandInterface $command): void
throw AccessDenied::becauseCommandIsNotGranted($command, $privilege->getReason());
}

$this->resetRuntimeCaches(disable: true);
$toPublish = $this->commandBus->handle($command);
$correlationId = CorrelationId::fromString(sprintf('%s_%s', substr($command::class, strrpos($command::class, '\\') + 1, 20), bin2hex(random_bytes(9))));

Expand Down Expand Up @@ -143,13 +152,13 @@ public function handle(CommandInterface $command): void
throw CatchUpHadErrors::createFromErrors($fullCatchUpResult->errors);
}
}
$this->resetRuntimeCaches();
$additionalCommands = $this->commandHook->onAfterHandle($command, $publishedEvents);
foreach ($additionalCommands as $additionalCommand) {
$this->handle($additionalCommand);
}
}


/**
* @template T of ProjectionStateInterface
* @param class-string<T> $projectionStateClassName
Expand All @@ -170,6 +179,9 @@ public function getContentGraph(WorkspaceName $workspaceName): ContentGraphInter
if (!$privilege->granted) {
throw AccessDenied::becauseWorkspaceCantBeRead($workspaceName, $privilege->getReason());
}
if ($this->contentGraphsRuntimeCache !== false) {
return $this->contentGraphsRuntimeCache[$workspaceName->value] ??= $this->contentGraphReadModel->getContentGraph($workspaceName);
}
return $this->contentGraphReadModel->getContentGraph($workspaceName);
}

Expand All @@ -192,6 +204,9 @@ public function getContentSubgraph(WorkspaceName $workspaceName, DimensionSpaceP
*/
public function findWorkspaceByName(WorkspaceName $workspaceName): ?Workspace
{
if ($this->workspacesRuntimeCache) {
return $this->workspacesRuntimeCache->get($workspaceName);
}
return $this->contentGraphReadModel->findWorkspaceByName($workspaceName);
}

Expand All @@ -201,6 +216,9 @@ public function findWorkspaceByName(WorkspaceName $workspaceName): ?Workspace
*/
public function findWorkspaces(): Workspaces
{
if ($this->workspacesRuntimeCache !== false) {
return $this->workspacesRuntimeCache ??= $this->contentGraphReadModel->findWorkspaces();
}
return $this->contentGraphReadModel->findWorkspaces();
}

Expand All @@ -219,6 +237,15 @@ public function getContentDimensionSource(): ContentDimensionSourceInterface
return $this->contentDimensionSource;
}

/**
* @param bool $disable to fully reset & deactivate the caches. This is required to deliver correct (uncached) results in case a catchup hook accesses this content repository.
*/
private function resetRuntimeCaches(bool $disable = false): void
{
$this->workspacesRuntimeCache = $disable ? false : null;
$this->contentGraphsRuntimeCache = $disable ? false : [];
}

private function enrichAndNormalizeEvents(DomainEvents $events, CorrelationId $correlationId): Events
{
$initiatingUserId = $this->authProvider->getAuthenticatedUserId() ?? UserId::forSystemUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ interface ContentGraphReadModelInterface extends ProjectionStateInterface
{
/**
* @throws WorkspaceDoesNotExist if the workspace does not exist
* todo cache instances to reduce queries (revert https://github.com/neos/neos-development-collection/pull/5246)
*/
public function getContentGraph(WorkspaceName $workspaceName): ContentGraphInterface;

Expand Down