How to flush neos content page cache with PHP (backend module controller)


I have a Neos (3.x) backend module and I’m wondering now how I can flush the cache of a Neos.Neos:Page NodeType in my PHP BackendController.

I have to keep the session and everything else. Only the cache for Page object should be flushed or marked as invalide so that the Page is build again from scratch.

Thank you,

Hi :slight_smile:

The package Neos.Neos uses a signal/slot combination, whenever a node is changed/published/deleted/[whatever] and it seems to use a ContentCacheFlusher class located in Neos\Neos\Fusion\Cache\ContentCacheFlusher.

        $dispatcher->connect(Node::class, 'nodeUpdated', ContentCacheFlusher::class, 'registerNodeChange');
        $dispatcher->connect(Node::class, 'nodeAdded', ContentCacheFlusher::class, 'registerNodeChange');
        $dispatcher->connect(Node::class, 'nodeRemoved', ContentCacheFlusher::class, 'registerNodeChange');
        $dispatcher->connect(Node::class, 'beforeNodeMove', ContentCacheFlusher::class, 'registerNodeChange');

And it seems that you can pass a node to it - so you task is to use a nodedata repository and get all nodes of a specific type, and then iterate that collection and use the ContentCacheFlusher class.

Bonus info: The caches are defined inside Caches.yaml in the Neos.Neos package so you might find some luck there about the configuration.

To flush a specific nodetype you need something like this:

 * @Flow\Inject
 * @var Neos\Flow\Cache\CacheManager
protected $cacheManager;

$this->cacheManager->flushCachesByTag($this->sanitizeTag('NodeType_' . $nodeType), true);

SanitizeTag is a simple return strtr($tag, ‘.:’, ‘_-’); and $nodeType is the name of your NodeType.

This just builds the FlushTag just as you would use it in Fusion.

If you need to flush the cache if a node has changed go the way @sorenmalling described. We use something similar to send ban requests to varnish/nginx

If you need to persist your session cache btw you should mark it as persistent:
persistent: TRUE

persistent: TRUE

Thank you to both of you!

I have used stolle’s example as it was exactly what I’ve needed.