13
13
14
14
use Neos \ContentRepository \Domain \Model \Workspace ;
15
15
use Neos \ContentRepository \Domain \Repository \WorkspaceRepository ;
16
+ use Neos \ContentRepository \Exception \NodeTypeNotFoundException ;
16
17
use Neos \Flow \Annotations as Flow ;
17
18
use Neos \Flow \Log \Utility \LogEnvironment ;
18
19
use Neos \Flow \Persistence \PersistenceManagerInterface ;
@@ -54,7 +55,7 @@ class ContentCacheFlusher
54
55
protected $ systemLogger ;
55
56
56
57
/**
57
- * @var array
58
+ * @var array<string, string>
58
59
*/
59
60
protected $ tagsToFlush = [];
60
61
@@ -109,18 +110,22 @@ class ContentCacheFlusher
109
110
*/
110
111
protected $ securityContext ;
111
112
113
+ /**
114
+ * @Flow\InjectConfiguration(path="fusion.contentCacheDebugMode")
115
+ * @var bool
116
+ */
117
+ protected $ debugMode ;
118
+
112
119
/**
113
120
* Register a node change for a later cache flush. This method is triggered by a signal sent via ContentRepository's Node
114
121
* model or the Neos Publishing Service.
115
122
*
116
123
* @param NodeInterface $node The node which has changed in some way
117
- * @param Workspace $targetWorkspace An optional workspace to flush
118
- * @return void
119
- * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
124
+ * @param Workspace|null $targetWorkspace An optional workspace to flush
120
125
*/
121
126
public function registerNodeChange (NodeInterface $ node , Workspace $ targetWorkspace = null ): void
122
127
{
123
- $ this ->tagsToFlush [ ContentCache::TAG_EVERYTHING ] = 'which were tagged with "Everything". ' ;
128
+ $ this ->addTagToFlush ( ContentCache::TAG_EVERYTHING , 'which were tagged with "Everything". ' ) ;
124
129
125
130
if (empty ($ this ->workspacesToFlush [$ node ->getWorkspace ()->getName ()])) {
126
131
$ this ->resolveWorkspaceChain ($ node ->getWorkspace ());
@@ -140,10 +145,11 @@ public function registerNodeChange(NodeInterface $node, Workspace $targetWorkspa
140
145
}
141
146
}
142
147
143
- /**
144
- * @param NodeInterface $node
145
- * @param Workspace $workspace
146
- */
148
+ protected function addTagToFlush (string $ tag , string $ message = '' ): void
149
+ {
150
+ $ this ->tagsToFlush [$ tag ] = $ this ->debugMode ? $ message : '' ;
151
+ }
152
+
147
153
protected function registerAllTagsToFlushForNodeInWorkspace (NodeInterface $ node , Workspace $ workspace ): void
148
154
{
149
155
$ nodeIdentifier = $ node ->getIdentifier ();
@@ -168,32 +174,23 @@ protected function registerAllTagsToFlushForNodeInWorkspace(NodeInterface $node,
168
174
break ;
169
175
}
170
176
$ tagName = 'DescendantOf_ ' . $ workspaceHash . '_ ' . $ nodeInWorkspace ->getIdentifier ();
171
- $ this ->tagsToFlush [ $ tagName] = sprintf ('which were tagged with "%s" because node "%s" has changed. ' , $ tagName , $ node ->getPath ());
177
+ $ this ->addTagToFlush ( $ tagName, sprintf ('which were tagged with "%s" because node "%s" has changed. ' , $ tagName , $ node ->getPath () ));
172
178
173
179
$ legacyTagName = 'DescendantOf_ ' . $ nodeInWorkspace ->getIdentifier ();
174
- $ this ->tagsToFlush [ $ legacyTagName] = sprintf ('which were tagged with legacy "%s" because node "%s" has changed. ' , $ legacyTagName , $ node ->getPath ());
180
+ $ this ->addTagToFlush ( $ legacyTagName, sprintf ('which were tagged with legacy "%s" because node "%s" has changed. ' , $ legacyTagName , $ node ->getPath () ));
175
181
}
176
182
}
177
183
}
178
184
179
- /**
180
- * @param Workspace $workspace
181
- * @return void
182
- */
183
- protected function resolveWorkspaceChain (Workspace $ workspace )
185
+ protected function resolveWorkspaceChain (Workspace $ workspace ): void
184
186
{
185
187
$ cachingHelper = $ this ->getCachingHelper ();
186
188
187
189
$ this ->workspacesToFlush [$ workspace ->getName ()][$ workspace ->getName ()] = $ cachingHelper ->renderWorkspaceTagForContextNode ($ workspace ->getName ());
188
190
$ this ->resolveTagsForChildWorkspaces ($ workspace , $ workspace ->getName ());
189
191
}
190
192
191
- /**
192
- * @param Workspace $workspace
193
- * @param string $startingPoint
194
- * @return void
195
- */
196
- protected function resolveTagsForChildWorkspaces (Workspace $ workspace , string $ startingPoint )
193
+ protected function resolveTagsForChildWorkspaces (Workspace $ workspace , string $ startingPoint ): void
197
194
{
198
195
$ cachingHelper = $ this ->getCachingHelper ();
199
196
$ this ->workspacesToFlush [$ startingPoint ][$ workspace ->getName ()] = $ cachingHelper ->renderWorkspaceTagForContextNode ($ workspace ->getName ());
@@ -210,54 +207,46 @@ protected function resolveTagsForChildWorkspaces(Workspace $workspace, string $s
210
207
* Pleas use registerNodeChange() if possible. This method is a low-level api. If you do use this method make sure
211
208
* that $cacheIdentifier contains the workspacehash as well as the node identifier: $workspaceHash .'_'. $nodeIdentifier
212
209
* The workspacehash can be received via $this->getCachingHelper()->renderWorkspaceTagForContextNode($workpsacename)
213
- *
214
- * @param string $cacheIdentifier
215
210
*/
216
- public function registerChangeOnNodeIdentifier ($ cacheIdentifier )
211
+ public function registerChangeOnNodeIdentifier (string $ cacheIdentifier ): void
217
212
{
218
- $ this ->tagsToFlush [ ContentCache::TAG_EVERYTHING ] = 'which were tagged with "Everything". ' ;
219
- $ this ->tagsToFlush [ 'Node_ ' . $ cacheIdentifier] = sprintf ('which were tagged with "Node_%s" because that identifier has changed. ' , $ cacheIdentifier );
220
- $ this ->tagsToFlush [ 'NodeDynamicTag_ ' . $ cacheIdentifier] = sprintf ('which were tagged with "NodeDynamicTag_%s" because that identifier has changed. ' , $ cacheIdentifier );
213
+ $ this ->addTagToFlush ( ContentCache::TAG_EVERYTHING , 'which were tagged with "Everything". ' ) ;
214
+ $ this ->addTagToFlush ( 'Node_ ' . $ cacheIdentifier, sprintf ('which were tagged with "Node_%s" because that identifier has changed. ' , $ cacheIdentifier) );
215
+ $ this ->addTagToFlush ( 'NodeDynamicTag_ ' . $ cacheIdentifier, sprintf ('which were tagged with "NodeDynamicTag_%s" because that identifier has changed. ' , $ cacheIdentifier) );
221
216
222
217
// Note, as we don't have a node here we cannot go up the structure.
223
218
$ tagName = 'DescendantOf_ ' . $ cacheIdentifier ;
224
- $ this ->tagsToFlush [ $ tagName] = sprintf ('which were tagged with "%s" because node "%s" has changed. ' , $ tagName , $ cacheIdentifier );
219
+ $ this ->addTagToFlush ( $ tagName, sprintf ('which were tagged with "%s" because node "%s" has changed. ' , $ tagName , $ cacheIdentifier) );
225
220
}
226
221
227
222
/**
228
223
* This is a low-level api. Please use registerNodeChange() if possible. Otherwise make sure that $nodeTypePrefix
229
224
* is set up correctly and contains the workspacehash wich can be received via
230
225
* $this->getCachingHelper()->renderWorkspaceTagForContextNode($workpsacename)
231
226
*
232
- * @param string $nodeTypeName
233
- * @param string $referenceNodeIdentifier
234
- * @param string $nodeTypePrefix
235
- *
236
- * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
227
+ * @throws NodeTypeNotFoundException
237
228
*/
238
- public function registerChangeOnNodeType ($ nodeTypeName , $ referenceNodeIdentifier = null , $ nodeTypePrefix = '' )
229
+ public function registerChangeOnNodeType (string $ nodeTypeName , string $ referenceNodeIdentifier = null , string $ nodeTypePrefix = '' ): void
239
230
{
240
- $ this ->tagsToFlush [ ContentCache::TAG_EVERYTHING ] = 'which were tagged with "Everything". ' ;
231
+ $ this ->addTagToFlush ( ContentCache::TAG_EVERYTHING , 'which were tagged with "Everything". ' ) ;
241
232
242
233
$ nodeTypesToFlush = $ this ->getAllImplementedNodeTypeNames ($ this ->nodeTypeManager ->getNodeType ($ nodeTypeName ));
243
234
244
- if (strlen ( $ nodeTypePrefix) > 0 ) {
235
+ if ($ nodeTypePrefix !== '' ) {
245
236
$ nodeTypePrefix = rtrim ($ nodeTypePrefix , '_ ' ) . '_ ' ;
246
237
}
247
238
248
239
foreach ($ nodeTypesToFlush as $ nodeTypeNameToFlush ) {
249
- $ this ->tagsToFlush [ 'NodeType_ ' . $ nodeTypePrefix . $ nodeTypeNameToFlush] = sprintf ('which were tagged with "NodeType_%s" because node "%s" has changed and was of type "%s". ' , $ nodeTypeNameToFlush , ($ referenceNodeIdentifier ? $ referenceNodeIdentifier : '' ), $ nodeTypeName );
240
+ $ this ->addTagToFlush ( 'NodeType_ ' . $ nodeTypePrefix . $ nodeTypeNameToFlush, sprintf ('which were tagged with "NodeType_%s" because node "%s" has changed and was of type "%s". ' , $ nodeTypeNameToFlush , ($ referenceNodeIdentifier ? $ referenceNodeIdentifier : '' ), $ nodeTypeName) );
250
241
}
251
242
}
252
243
253
244
/**
254
245
* Fetches possible usages of the asset and registers nodes that use the asset as changed.
255
246
*
256
- * @param AssetInterface $asset
257
- * @return void
258
- * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
247
+ * @throws NodeTypeNotFoundException
259
248
*/
260
- public function registerAssetChange (AssetInterface $ asset )
249
+ public function registerAssetChange (AssetInterface $ asset ): void
261
250
{
262
251
// In Nodes only assets are referenced, never asset variants directly. When an asset
263
252
// variant is updated, it is passed as $asset, but since it is never "used" by any node
@@ -293,31 +282,35 @@ public function registerAssetChange(AssetInterface $asset)
293
282
$ assetIdentifier = $ this ->persistenceManager ->getIdentifierByObject ($ asset );
294
283
// @see RuntimeContentCache.addTag
295
284
$ tagName = 'AssetDynamicTag_ ' . $ workspaceHash . '_ ' . $ assetIdentifier ;
296
- $ this ->tagsToFlush [ $ tagName] = sprintf ('which were tagged with "%s" because asset "%s" has changed. ' , $ tagName , $ assetIdentifier );
285
+ $ this ->addTagToFlush ( $ tagName, sprintf ('which were tagged with "%s" because asset "%s" has changed. ' , $ tagName , $ assetIdentifier) );
297
286
}
298
287
}
299
288
289
+ public function shutdownObject (): void
290
+ {
291
+ $ this ->commit ();
292
+ }
293
+
300
294
/**
301
295
* Flush caches according to the previously registered node changes.
302
- *
303
- * @return void
304
296
*/
305
- public function shutdownObject ()
297
+ protected function commit (): void
306
298
{
307
299
if ($ this ->tagsToFlush !== []) {
308
- foreach ($ this ->tagsToFlush as $ tag => $ logMessage ) {
309
- $ affectedEntries = $ this ->contentCache ->flushByTag ($ tag );
310
- if ($ affectedEntries > 0 ) {
311
- $ this ->systemLogger ->debug (sprintf ('Content cache: Removed %s entries %s ' , $ affectedEntries , $ logMessage ));
300
+ if ($ this ->debugMode ) {
301
+ foreach ($ this ->tagsToFlush as $ tag => $ logMessage ) {
302
+ $ affectedEntries = $ this ->contentCache ->flushByTag ($ tag );
303
+ if ($ affectedEntries > 0 ) {
304
+ $ this ->systemLogger ->debug (sprintf ('Content cache: Removed %s entries %s ' , $ affectedEntries , $ logMessage ));
305
+ }
312
306
}
307
+ } else {
308
+ $ affectedEntries = $ this ->contentCache ->flushByTags (array_keys ($ this ->tagsToFlush ));
309
+ $ this ->systemLogger ->debug (sprintf ('Content cache: Removed %s entries ' , $ affectedEntries ));
313
310
}
314
311
}
315
312
}
316
313
317
- /**
318
- * @param AssetUsageInNodeProperties $assetUsage
319
- * @return ContentContext
320
- */
321
314
protected function getContextForReference (AssetUsageInNodeProperties $ assetUsage ): ContentContext
322
315
{
323
316
$ hash = md5 (sprintf ('%s-%s ' , $ assetUsage ->getWorkspaceName (), json_encode ($ assetUsage ->getDimensionValues ())));
@@ -334,10 +327,9 @@ protected function getContextForReference(AssetUsageInNodeProperties $assetUsage
334
327
}
335
328
336
329
/**
337
- * @param NodeType $nodeType
338
330
* @return array<string>
339
331
*/
340
- protected function getAllImplementedNodeTypeNames (NodeType $ nodeType )
332
+ protected function getAllImplementedNodeTypeNames (NodeType $ nodeType ): array
341
333
{
342
334
$ self = $ this ;
343
335
$ types = array_reduce ($ nodeType ->getDeclaredSuperTypes (), function (array $ types , NodeType $ superType ) use ($ self ) {
@@ -348,9 +340,6 @@ protected function getAllImplementedNodeTypeNames(NodeType $nodeType)
348
340
return $ types ;
349
341
}
350
342
351
- /**
352
- * @return CachingHelper
353
- */
354
343
protected function getCachingHelper (): CachingHelper
355
344
{
356
345
if (!$ this ->cachingHelper instanceof CachingHelper) {
0 commit comments