/
ContentCacheFlusher.php
304 lines (267 loc) · 11.9 KB
/
ContentCacheFlusher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
declare(strict_types=1);
namespace Neos\Neos\Fusion\Cache;
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFoundException;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\Flow\Annotations as Flow;
use Neos\Fusion\Core\Cache\ContentCache;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\AssetVariantInterface;
use Psr\Log\LoggerInterface;
/**
* This service flushes Fusion content caches triggered by node changes.
*
* It is called when the projection changes: In this case, it is triggered by
* {@see GraphProjectorCatchUpHookForCacheFlushing} which calls this method..
* This is the relevant case if publishing a workspace
* - where we f.e. need to flush the cache for Live.
*
* @Flow\Scope("singleton")
*/
class ContentCacheFlusher
{
#[Flow\InjectConfiguration(path: "fusion.contentCacheDebugMode")]
protected bool $debugMode;
public function __construct(
protected ContentCache $contentCache,
protected LoggerInterface $systemLogger,
) {
}
/**
* Main entry point to *directly* flush the caches of a given NodeAggregate
*
* @param ContentRepository $contentRepository
* @param ContentStreamId $contentStreamId
* @param NodeAggregateId $nodeAggregateId
*/
public function flushNodeAggregate(
ContentRepository $contentRepository,
ContentStreamId $contentStreamId,
NodeAggregateId $nodeAggregateId
): void {
$tagsToFlush = [];
$tagsToFlush[ContentCache::TAG_EVERYTHING] = 'which were tagged with "Everything".';
$this->registerChangeOnNodeIdentifier($contentRepository->id, $contentStreamId, $nodeAggregateId, $tagsToFlush);
$nodeAggregate = $contentRepository->getContentGraph()->findNodeAggregateById(
$contentStreamId,
$nodeAggregateId
);
if (!$nodeAggregate) {
// Node Aggregate was removed in the meantime, so no need to clear caches on this one anymore.
return;
}
$this->registerChangeOnNodeType(
$nodeAggregate->nodeTypeName,
$contentRepository->id,
$contentStreamId,
$nodeAggregateId,
$tagsToFlush,
$contentRepository
);
$parentNodeAggregates = [];
foreach (
$contentRepository->getContentGraph()->findParentNodeAggregates(
$contentStreamId,
$nodeAggregateId
) as $parentNodeAggregate
) {
$parentNodeAggregates[] = $parentNodeAggregate;
}
// we do not need these variables anymore here
unset($contentStreamId, $nodeAggregateId);
// NOTE: Normally, the content graph cannot contain cycles. However, during the
// testcase "Features/ProjectionIntegrityViolationDetection/AllNodesAreConnectedToARootNodePerSubgraph.feature"
// and in case of bugs, it could have actually cycles.
// We still want the content cache flushing to work, without hanging up in an endless loop.
// That's why we track the seen NodeAggregateIds to be sure we don't travers them multiple times.
$processedNodeAggregateIds = [];
while ($nodeAggregate = array_shift($parentNodeAggregates)) {
assert($nodeAggregate instanceof NodeAggregate);
if (isset($processedNodeAggregateIds[$nodeAggregate->nodeAggregateId->value])) {
// we've already processed this NodeAggregateId (i.e. flushed the caches for it);
// thus we can skip this one, and thus break the cycle.
continue;
}
$processedNodeAggregateIds[$nodeAggregate->nodeAggregateId->value] = true;
$tagName = CacheTag::forDescendantOfNode($contentRepository->id, $nodeAggregate->contentStreamId, $nodeAggregate->nodeAggregateId);
$tagsToFlush[$tagName->value] = sprintf(
'which were tagged with "%s" because node "%s" has changed.',
$tagName->value,
$nodeAggregate->nodeAggregateId->value
);
foreach (
$contentRepository->getContentGraph()->findParentNodeAggregates(
$nodeAggregate->contentStreamId,
$nodeAggregate->nodeAggregateId
) as $parentNodeAggregate
) {
$parentNodeAggregates[] = $parentNodeAggregate;
}
}
$this->flushTags($tagsToFlush);
}
/**
* Please use registerNodeChange() if possible. This method is a low-level api. If you do use this method make sure
* that $cacheIdentifier contains the workspacehash as well as the node identifier:
* $workspaceHash .'_'. $nodeIdentifier
* The workspacehash can be received via $this->getCachingHelper()->renderWorkspaceTagForContextNode($workpsacename)
*
* @param array<string,string> &$tagsToFlush
*/
private function registerChangeOnNodeIdentifier(
ContentRepositoryId $contentRepositoryId,
ContentStreamId $contentStreamId,
NodeAggregateId $nodeAggregateId,
array &$tagsToFlush
): void {
$nodeCacheIdentifier = CacheTag::forNodeAggregate($contentRepositoryId, $contentStreamId, $nodeAggregateId);
$tagsToFlush[$nodeCacheIdentifier->value] = sprintf(
'which were tagged with "%s" because that identifier has changed.',
$nodeCacheIdentifier->value
);
$descandantOfNodeCacheIdentifier = CacheTag::forDescendantOfNode($contentRepositoryId, $contentStreamId, $nodeAggregateId);
$tagsToFlush[$descandantOfNodeCacheIdentifier->value] = sprintf(
'which were tagged with "%s" because node "%s" has changed.',
$descandantOfNodeCacheIdentifier->value,
$nodeCacheIdentifier->value
);
}
/**
* This is a low-level api. Please use registerNodeChange() if possible. Otherwise make sure that $nodeTypePrefix
* is set up correctly and contains the workspacehash wich can be received via
* $this->getCachingHelper()->renderWorkspaceTagForContextNode($workpsacename)
*
* @param array<string,string> &$tagsToFlush
*/
private function registerChangeOnNodeType(
NodeTypeName $nodeTypeName,
ContentRepositoryId $contentRepositoryId,
ContentStreamId $contentStreamId,
?NodeAggregateId $referenceNodeIdentifier,
array &$tagsToFlush,
ContentRepository $contentRepository
): void {
$nodeType = $contentRepository->getNodeTypeManager()->getNodeType($nodeTypeName);
if ($nodeType) {
$nodeTypesNamesToFlush = $this->getAllImplementedNodeTypeNames($nodeType);
} else {
// as a fallback, we flush the single NodeType
$nodeTypesNamesToFlush = [$nodeTypeName->value];
}
foreach ($nodeTypesNamesToFlush as $nodeTypeNameToFlush) {
$nodeTypeNameCacheIdentifier = CacheTag::forNodeTypeName($contentRepositoryId, $contentStreamId, NodeTypeName::fromString($nodeTypeNameToFlush));
$tagsToFlush[$nodeTypeNameCacheIdentifier->value] = sprintf(
'which were tagged with "%s" because node "%s" has changed and was of type "%s".',
$nodeTypeNameCacheIdentifier->value,
($referenceNodeIdentifier?->value ?? ''),
$nodeTypeName->value
);
}
}
/**
* Flush caches according to the previously registered node changes.
*
* @param array<string,string> $tagsToFlush
*/
protected function flushTags(array $tagsToFlush): void
{
if ($this->debugMode) {
foreach ($tagsToFlush as $tag => $logMessage) {
$affectedEntries = $this->contentCache->flushByTag($tag);
if ($affectedEntries > 0) {
$this->systemLogger->debug(sprintf(
'Content cache: Removed %s entries %s',
$affectedEntries,
$logMessage
));
}
}
} else {
$affectedEntries = $this->contentCache->flushByTags(array_keys($tagsToFlush));
$this->systemLogger->debug(sprintf('Content cache: Removed %s entries', $affectedEntries));
}
}
/**
* @param NodeType $nodeType
* @return array<string>
*/
protected function getAllImplementedNodeTypeNames(NodeType $nodeType): array
{
$self = $this;
$types = array_reduce(
$nodeType->getDeclaredSuperTypes(),
function (array $types, NodeType $superType) use ($self) {
return array_merge($types, $self->getAllImplementedNodeTypeNames($superType));
},
[$nodeType->name->value]
);
return array_unique($types);
}
/**
* Fetches possible usages of the asset and registers nodes that use the asset as changed.
*
* @throws NodeTypeNotFoundException
*/
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 us the original asset
// of the variant.
if ($asset instanceof AssetVariantInterface) {
$asset = $asset->getOriginalAsset();
}
// TODO: re-implement this based on the code below
/*
if (!$asset->isInUse()) {
return;
}
$cachingHelper = $this->getCachingHelper();
foreach ($this->assetService->getUsageReferences($asset) as $reference) {
if (!$reference instanceof AssetUsageInNodeProperties) {
continue;
}
$workspaceHash = $cachingHelper->renderWorkspaceTagForContextNode($reference->getWorkspaceName());
$this->securityContext->withoutAuthorizationChecks(function () use ($reference, &$node) {
$node = $this->getContextForReference($reference)->getNodeByIdentifier($reference->getNodeIdentifier());
});
if (!$node instanceof Node) {
$this->systemLogger->warning(sprintf(
'Found a node reference from node with identifier %s in workspace %s to asset %s,'
. ' but the node could not be fetched.',
$reference->getNodeIdentifier(),
$reference->getWorkspaceName(),
$this->persistenceManager->getIdentifierByObject($asset)
), LogEnvironment::fromMethodName(__METHOD__));
continue;
}
$this->registerNodeChange($node);
$assetIdentifier = $this->persistenceManager->getIdentifierByObject($asset);
// @see RuntimeContentCache.addTag
$tagName = 'AssetDynamicTag_' . $workspaceHash . '_' . $assetIdentifier;
$this->addTagToFlush(
$tagName,
sprintf(
'which were tagged with "%s" because asset "%s" has changed.',
$tagName,
$assetIdentifier
)
);
}*/
}
}