1

I see in the docs I can do something like:

{% set entries = craft.entries
  .sectionId(2)
  .with(['someRelationField'])
  .cache(0)
  .all() 
%}

and this will cache the query indefinitely because of .cache(0).

What if I would like to cache some matrix blocks in a global set? For example:

{% set blocks = myGlobalSet
  .myMatrixField
  .with(['someRelationField'])
  .cache(0)
  .all() 
%}

Would that cache that query like I believe it will or, since it's a relation to the global set, will it mess with the query is some way?

1 Answer 1

2

.cache() is a rather low-level method defined in yii\db\Command, that sets class-level cache duration and dependency properties:
https://github.com/yiisoft/yii2/blob/5406e5dad3eaa4e9d58f65bb565d37adc4307f3b/framework/db/Command.php#L117-L130

Although Craft registers an ElementQueryTagDependency for you by default, the cache properties know nothing about Craft and are subsequently used internally in the Command::queryInternal method to cache the result of an executed query: https://github.com/yiisoft/yii2/blob/5406e5dad3eaa4e9d58f65bb565d37adc4307f3b/framework/db/Command.php#L1186-L1189

So, the answer to your question is that the query result will be cached as expected – the caching mechanism knows nothing of relations to global sets and so doesn’t discriminate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.