forked from neos/neos-development-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Overhaul ContentCacheFlusher
- Loading branch information
Showing
5 changed files
with
148 additions
and
88 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
Neos.Neos/Classes/Fusion/Cache/AssetChangeHandlerForCacheFlushing.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Neos\Neos\Fusion\Cache; | ||
|
||
use Neos\Media\Domain\Model\AssetInterface; | ||
use Neos\Media\Domain\Model\AssetVariantInterface; | ||
use Neos\Neos\AssetUsage\Dto\AssetUsageFilter; | ||
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId; | ||
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds; | ||
use Neos\Neos\AssetUsage\GlobalAssetUsageService; | ||
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry; | ||
use Neos\Flow\Persistence\PersistenceManagerInterface; | ||
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName; | ||
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate; | ||
use Neos\ContentRepository\Core\ContentRepository; | ||
|
||
class AssetChangeHandlerForCacheFlushing | ||
{ | ||
public function __construct( | ||
protected readonly GlobalAssetUsageService $globalAssetUsageService, | ||
protected readonly ContentRepositoryRegistry $contentRepositoryRegistry, | ||
protected readonly PersistenceManagerInterface $persistenceManager, | ||
protected readonly ContentCacheFlusher $contentCacheFlusher, | ||
) { | ||
} | ||
|
||
/** | ||
* Fetches possible usages of the asset and registers nodes that use the asset as changed. | ||
*/ | ||
public function registerAssetChange(AssetInterface $asset): void | ||
{ | ||
// In Nodes only assets are referenced, never asset variants directly. When an asset | ||
// variant is updated, it is passed as $asset, but since it is never "used" by any node | ||
// no flushing of corresponding entries happens. Thus we instead use the original asset | ||
// of the variant. | ||
if ($asset instanceof AssetVariantInterface) { | ||
$asset = $asset->getOriginalAsset(); | ||
} | ||
|
||
$filter = AssetUsageFilter::create() | ||
->withAsset($this->persistenceManager->getIdentifierByObject($asset)) | ||
->includeVariantsOfAsset(); | ||
|
||
$workspaceNamesByContentStreamId = []; | ||
foreach ($this->globalAssetUsageService->findByFilter($filter) as $contentRepositoryId => $usages) { | ||
$contentRepository = $this->contentRepositoryRegistry->get(ContentRepositoryId::fromString($contentRepositoryId)); | ||
foreach ($usages as $usage) { | ||
// TODO: Remove when WorkspaceName is part of the AssetUsageProjection | ||
$workspaceName = $workspaceNamesByContentStreamId[$contentRepositoryId][$usage->contentStreamId->value] ?? null; | ||
if ($workspaceName === null) { | ||
$workspace = $contentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId($usage->contentStreamId); | ||
if ($workspace === null) { | ||
continue; | ||
} | ||
$workspaceName = $workspace->workspaceName; | ||
$workspaceNamesByContentStreamId[$contentRepositoryId][$usage->contentStreamId->value] = $workspaceName; | ||
} | ||
// | ||
|
||
$nodeAggregate = $contentRepository->getContentGraph($workspaceName)->findNodeAggregateById($usage->nodeAggregateId); | ||
if ($nodeAggregate === null) { | ||
continue; | ||
} | ||
$flushNodeAggregateRequest = FlushNodeAggregateRequest::create( | ||
$contentRepository->id, | ||
$workspaceName, | ||
$nodeAggregate->nodeAggregateId, | ||
$nodeAggregate->nodeTypeName, | ||
$this->determineParentNodeAggregateIds($contentRepository, $workspaceName, $nodeAggregate->nodeAggregateId), | ||
); | ||
$this->contentCacheFlusher->flushNodeAggregate($flushNodeAggregateRequest, CacheFlushingStrategy::ON_SHUTDOWN); | ||
} | ||
} | ||
} | ||
|
||
private function determineParentNodeAggregateIds(ContentRepository $contentRepository, WorkspaceName $workspaceName, NodeAggregateId $childNodeAggregateId): NodeAggregateIds | ||
{ | ||
$parentNodeAggregates = $contentRepository->getContentGraph($workspaceName)->findParentNodeAggregates($childNodeAggregateId); | ||
$parentNodeAggregateIds = NodeAggregateIds::fromArray( | ||
array_map(static fn (NodeAggregate $parentNodeAggregate) => $parentNodeAggregate->nodeAggregateId, iterator_to_array($parentNodeAggregates)) | ||
); | ||
|
||
foreach ($parentNodeAggregateIds as $parentNodeAggregateId) { | ||
// Prevent infinite loops | ||
if (!$parentNodeAggregateIds->contain($parentNodeAggregateId)) { | ||
$parentNodeAggregateIds->merge($this->determineParentNodeAggregateIds($contentRepository, $workspaceName, $parentNodeAggregateId)); | ||
} | ||
} | ||
|
||
return $parentNodeAggregateIds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Neos\Fusion\Cache; | ||
|
||
enum CacheFlushingStrategy | ||
{ | ||
case IMMEDIATELY; | ||
case ON_SHUTDOWN; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters