Is it possible to clear a specific template cache using a specific key since the template cache refactor in Craft CMS 3.5+? #15745
-
I have a scenario where we I'm using the In Craft CMS 3, you could do: Craft::$app->getTemplateCaches()->deleteCachesByKey('key'); This however was deprecated and doesn't exist in Craft CMS 5, is there a equivalent way to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can register a custom cache invalidation tag to the cache: {% cache %}
{% do craft.app.elements.collectCacheTags(['custom-tag']) %}
...
{% endcache %} Then clear caches by that tag in your queue job: use yii\caching\TagDependency;
TagDependency::invalidate(Craft::$app->cache, 'custom-tag'); For extra credit, you can register the tag with the Caches utility, which will add a checkbox for the tag under “Invalidate Data Caches”: use craft\base\Event;
use craft\events\RegisterCacheOptionsEvent;
use craft\utilities\ClearCaches;
Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_TAG_OPTIONS, function(RegisterCacheOptionsEvent $event) {
$event->options[] = [
'tag' => 'custom-tag',
'label' => 'My Custom Tag',
];
}); That will also add an |
Beta Was this translation helpful? Give feedback.
You can register a custom cache invalidation tag to the cache:
Then clear caches by that tag in your queue job:
For extra credit, you can register the tag with the Caches utility, which will add a checkbox for the tag under “Invalidate Data Caches”: