Search code examples
shopware6

Add custom tag to ReverseProxyCache


What is the correct way (if any) to add a custom tag to all cached pages? Should I decorate ReverseProxyCache and add my tag in the write method?

PS: I use Varnish as reverse proxy so VarnishReverseProxyGateway kicks in, but maybe this is not important


Solution

  • There are multiple instances of StoreApiRouteCacheTagsEvent which are dispatched in the respective class of the route. You may listen to these events and add tags to them in the subscriber. While there is no single event that covers all routes, at least all of them extend the same class, so you may use a single method for all of them.

    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingRouteCacheTagsEvent::class => 'onCacheTags',
            ProductSearchRouteCacheTagsEvent::class => 'onCacheTags',
            // ...
        ];
    }
    
    public function onCacheTags(StoreApiRouteCacheTagsEvent $event): void
    {
        $event->addTags(['my', 'custom', 'tags']);
    }