Answers.Body: <p><code>.cache()</code> is a rather low-level method defined in <code>yii\db\Command</code>, that sets class-level cache duration and dependency properties:<br />
  <a href="https://github.com/yiisoft/yii2/blob/5406e5dad3eaa4e9d58f65bb565d37adc4307f3b/framework/db/Command.php#L117-L130" rel="nofollow noreferrer">https://github.com/yiisoft/yii2/blob/5406e5dad3eaa4e9d58f65bb565d37adc4307f3b/framework/db/Command.php#L117-L130</a></p>
  <p>Although <a href="https://craftcms.com/docs/4.x/element-queries.html#caching-element-queries" rel="nofollow noreferrer">Craft registers an <code>ElementQueryTagDependency</code></a> for you by default, the cache properties know nothing about Craft and are subsequently used internally in the <code>Command::queryInternal</code> method to cache the result of an executed query:
  <a href="https://github.com/yiisoft/yii2/blob/5406e5dad3eaa4e9d58f65bb565d37adc4307f3b/framework/db/Command.php#L1186-L1189" rel="nofollow noreferrer">https://github.com/yiisoft/yii2/blob/5406e5dad3eaa4e9d58f65bb565d37adc4307f3b/framework/db/Command.php#L1186-L1189</a></p>
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-08T01:55:30.357Z
Answers.LastActivityDate: 2023-03-08T02:08:19.717Z
Answers.LastEditDate: 2023-03-08T02:08:19.717Z
Answers.UserDisplayName: Ben Croker
Body: <p>I see in the docs I can do something like:</p>
  <pre><code>{% set entries = craft.entries
    .sectionId(2)
    .with(['someRelationField'])
    .cache(0)
    .all() 
  %}
  </code></pre>
  <p>and this will cache the query indefinitely because of <code>.cache(0)</code>.</p>
  <p>What if I would like to cache some matrix blocks in a global set? For example:</p>
  <pre><code>{% set blocks = myGlobalSet
    .myMatrixField
    .with(['someRelationField'])
    .cache(0)
    .all() 
  %}
  </code></pre>
  <p>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?</p>
  
Tags: <cache><query><elementquery>
Title: Do cache queries work on relation fields and global sets?
ThreadText: # Do cache queries work on relation fields and global sets?
  
  (Asked by: username_0 on 2023-02-02)
  
  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?
  
  **Tags:** cache, query, elementquery
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-03-08. Score: 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`](https://craftcms.com/docs/4.x/element-queries.html#caching-element-queries) 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.
  
  
  ---
  
  