I'm using the twig cache in a Symfony 5.4 project. If I cache something like:
{% cache "customer_1" ttl(3600) tags('customer') %}
Some HTML here
{% endcache %}
I need to be able to invalidate the twig cache with tag customer from within a Symfony controller or service.
I've tried autoloading Symfony\Contracts\Cache\TagAwareCacheInterface $twigCache (found from bin/console debug:autowiring) but this doesn't seem to work.
With this I called $twigCache->invalidateTags(['customer']), but does not appear to do nothing.
Is it possible to access and clear the twig cache from the Symfony side?
Apparently, you are doing it correctly.
I have just set up a Symfony 5.4 project from scratch and tried it, and it works as expected.
E.g.:
And the template:
I have verified with
bin/console cache:pool:listthat the pooltwig.cachehas been created, so I can autowire that pool directly with$twigCache.If I access
/super-cache, the first time the template will be cached completely, and the "cached at" string won't change in following calls:Now accessing
/super-cache/foo, the controller will invalidate the tagfoofor the pool, and the template content will be refreshed accordingly (check the "created at" string on the first two blocks):Knowing that this should work as intended, you'll need to find if there is something else amiss in your setup.