That way plugins could initiate their own template recorders, and use the resulting collections for their own purposes, unrelated to Craft's own caching system (for example, they could be used as Varnish dependencies).
This is resolved for Craft 3.5. (885912438eb9c8929152552524d632bb0a6b514f)
Element queries now register cache invalidation tags based on the parameters that are set on them
Element types now define the tags that should be invalidated when they are saved/deleted
Anything that wants to start caching things that involve element queries can do so by calling craft\services\Elements::startCollectingCacheTags()
before executing the queries, and stopCollectingCacheTags()
afterwards, which will return a yii\caching\TagDependency
object that can be used as a cache dependency.
We鈥檙e using this new system for template caches ({% cache %}
tags) and GraphQL query caches. Here鈥檚 the GraphQL code, as that鈥檚 a simpler example, since it doesn鈥檛 have to worry about nested caches like template caches do:
@brandonkelly Great work! Just a heads-up, I was playing with this and if you don't create a query within the cache tag (for example, using the matched element (entry.title
) instead, the cache is not automatically invalidated when the corresponding entry saves.
Does not clear on entry save:
{% cache %}
{{ entry.title }}
{% endcache %}
Does clear on entry save:
{% cache %}
{% set entry = craft.entries.section('homepage').one() %}
<h1>{{ entry.test }}</h1>
{% endcache %}
@Mosnar I caught that yesterday as well. Should be fixed via decc3313bbbf63249bbf76903ed5f068fe3fd781.
Most helpful comment
This is resolved for Craft 3.5. (885912438eb9c8929152552524d632bb0a6b514f)
Element queries now register cache invalidation tags based on the parameters that are set on them
https://github.com/craftcms/cms/blob/885912438eb9c8929152552524d632bb0a6b514f/src/elements/db/EntryQuery.php#L1009-L1023
Element types now define the tags that should be invalidated when they are saved/deleted
https://github.com/craftcms/cms/blob/885912438eb9c8929152552524d632bb0a6b514f/src/elements/Entry.php#L839-L852
Anything that wants to start caching things that involve element queries can do so by calling
craft\services\Elements::startCollectingCacheTags()
before executing the queries, andstopCollectingCacheTags()
afterwards, which will return ayii\caching\TagDependency
object that can be used as a cache dependency.We鈥檙e using this new system for template caches (
{% cache %}
tags) and GraphQL query caches. Here鈥檚 the GraphQL code, as that鈥檚 a simpler example, since it doesn鈥檛 have to worry about nested caches like template caches do:https://github.com/craftcms/cms/blob/885912438eb9c8929152552524d632bb0a6b514f/src/services/Gql.php#L417-L440
https://github.com/craftcms/cms/blob/885912438eb9c8929152552524d632bb0a6b514f/src/services/Gql.php#L478-L488