Answers.Body: <p>Yes, sort of. As soon as the rendering template hits a <code>{% cache %}</code> tag, Craft grabs the outputted content from the cache table in the database and ignores all other template logic until the <code>{% endcache %}</code> tag. </p>
  
  <p>So, the <strong>results</strong> of the PHP-based query will be cached with the rest of whatever is between the tags only for use in that instance as HTML.</p>
  
  <p>If your hook is called anywhere else, Craft will make the PHP query again, as the query itself is not cached independently. In order to accomplish that (if you want), you will need to implement some plugin-level cacheing using the <a href="http://buildwithcraft.com/classreference/services/CacheService" rel="nofollow">Cache Service</a>.</p>
  
  <p>For cache invalidation, here is what Brandon has to say:</p>
  
  <p>Any time ElementCriteriaModel::find() et al. is called within <code>{% cache %}</code> tags, it will be recorded, however you will have to manually register any elements that should be associated with the cache, using this code:</p>
  
  <pre><code>// Don't initialize the CacheService if we don't have to
  $cacheService = craft()-&gt;getComponent('templateCache', false);
  
  if ($cacheService)
  {
      $cacheService-&gt;includeElementInTemplateCaches($elementId);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T17:08:16.530Z
Answers.Text: Great, thanks Aaron... That info on cache invalidation was exactly what I was looking for!
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T01:40:22.397Z
Answers.LastActivityDate: 2015-06-23T14:01:10.187Z
Answers.LastEditDate: 2015-06-23T14:01:10.187Z
Answers.UserDisplayName: Aaron Berkowitz
Answers.Body: <p>My understanding is that the <code>{% cache %}</code> tag operates like a conditional. If it succeeds (cache hit), then output the cached value. If it fails (cache miss), then calculate the value (run everything between the <code>{% cache %}</code> and <code>{% endcache %}</code> tags), cache it for later, and output it.</p>
  
  <p>So I don't think you need to do anything special. Your hook will be run (and the result cached) only when the code inside the <code>{% cache %}</code> tag is run, which is whenever there is a cache miss.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T17:09:22.090Z
Answers.Text: Thanks Marion, great answer! But I'm gonna give this one to Aaron, since he touched upon cache invalidation as well.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T17:12:59.020Z
Answers.Text: I would have given it to Aaron too
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T17:13:55.183Z
Answers.Text: Lol! I still very much appreciate the answer. :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-23T01:42:20.220Z
Answers.LastActivityDate: 2015-06-23T01:42:20.220Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <p>More than you ever wanted to know about the Craft <code>{% cache %}</code> tag:</p>
  
  <p><a href="https://nystudio107.com/blog/the-craft-cache-tag-in-depth" rel="nofollow noreferrer">The Craft {% cache %} Tag In-Depth</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-06T20:11:14.157Z
Answers.LastActivityDate: 2017-12-06T20:11:14.157Z
Answers.UserDisplayName: andrew.welch
Body: <p>A portion of my template is being cached, using the standard <a href="https://craftcms.com/docs/templating/cache" rel="nofollow noreferrer">cache tag</a>:</p>
  
  <pre><code>{% cache %}
  </code></pre>
  
  <p>Within that cached section of the template, I'm using a <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow noreferrer">template hook</a>:</p>
  
  <pre><code>{% hook 'myTemplateHook' %}
  </code></pre>
  
  <p>Within that template hook, I'm collecting entry data via a normal ElementCriteriaModel:</p>
  
  <pre class="lang-php prettyprint-override"><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'mySection';
  $myEntries = $criteria-&gt;find();
  </code></pre>
  
  <p>So I'm wondering... <strong>Will my PHP-based query be cached?</strong></p>
  
  <p>I'd like for the caching of this query to mimic any caching that would take place via Twig templates. <strong>What do I need to do to ensure this follows normal caching patterns?</strong></p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Great answers so far, but they don't really touch on the heart of my concern...</p>
  
  <p>Normal Twig-based caches actually record all <em>criteria</em> and <em>elements</em> used in the cached HTML, for the purpose of invalidating that cache when either of those things change. What I really want to know is, <strong>would my PHP-based criteria and resulting elements be recorded as well?</strong> Since the criteria is built in PHP, and the elements are collected in PHP, I'd like to know if they are being stored (as they normally would via Twig).</p>
  
  <p>This obviously makes a difference for cache invalidation... If an element fetched via PHP is updated or deleted, I'd like to be confident that the cache would be cleared as well.</p>
  
Tags: <plugin-development><templating><cache><elementcriteriamodel><hook>
Title: Template Hook + Caching
ThreadText: # Template Hook + Caching
  
  (Asked by: username_0 on 2015-06-22)
  
  A portion of my template is being cached, using the standard [cache tag](https://craftcms.com/docs/templating/cache):
  
  ```
  {% cache %}
  
  ```
  
  Within that cached section of the template, I'm using a [template hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail):
  
  ```
  {% hook 'myTemplateHook' %}
  
  ```
  
  Within that template hook, I'm collecting entry data via a normal ElementCriteriaModel:
  
  ```php
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'mySection';
  $myEntries = $criteria->find();
  
  ```
  
  So I'm wondering... **Will my PHP-based query be cached?**
  
  I'd like for the caching of this query to mimic any caching that would take place via Twig templates. **What do I need to do to ensure this follows normal caching patterns?**
  
  **EDIT:**
  
  Great answers so far, but they don't really touch on the heart of my concern...
  
  Normal Twig-based caches actually record all *criteria* and *elements* used in the cached HTML, for the purpose of invalidating that cache when either of those things change. What I really want to know is, **would my PHP-based criteria and resulting elements be recorded as well?** Since the criteria is built in PHP, and the elements are collected in PHP, I'd like to know if they are being stored (as they normally would via Twig).
  
  This obviously makes a difference for cache invalidation... If an element fetched via PHP is updated or deleted, I'd like to be confident that the cache would be cleared as well.
  
  **Tags:** plugin-development, templating, cache, elementcriteriamodel, hook
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2015-06-23. Score: 3 | Accepted answer
  
  Yes, sort of. As soon as the rendering template hits a `{% cache %}` tag, Craft grabs the outputted content from the cache table in the database and ignores all other template logic until the `{% endcache %}` tag.
  
  So, the **results** of the PHP-based query will be cached with the rest of whatever is between the tags only for use in that instance as HTML.
  
  If your hook is called anywhere else, Craft will make the PHP query again, as the query itself is not cached independently. In order to accomplish that (if you want), you will need to implement some plugin-level cacheing using the [Cache Service](http://buildwithcraft.com/classreference/services/CacheService).
  
  For cache invalidation, here is what Brandon has to say:
  
  Any time ElementCriteriaModel::find() et al. is called within `{% cache %}` tags, it will be recorded, however you will have to manually register any elements that should be associated with the cache, using this code:
  
  ```
  // Don't initialize the CacheService if we don't have to
  $cacheService = craft()->getComponent('templateCache', false);
  
  if ($cacheService)
  {
      $cacheService->includeElementInTemplateCaches($elementId);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Great, thanks username_1... That info on cache invalidation was exactly what I was looking for!  
  
  ---
  
  ### Answer by username_2 on 2015-06-23. Score: 2
  
  My understanding is that the `{% cache %}` tag operates like a conditional. If it succeeds (cache hit), then output the cached value. If it fails (cache miss), then calculate the value (run everything between the `{% cache %}` and `{% endcache %}` tags), cache it for later, and output it.
  
  So I don't think you need to do anything special. Your hook will be run (and the result cached) only when the code inside the `{% cache %}` tag is run, which is whenever there is a cache miss.
  
  > **Comments:**
  > **username_0**: Thanks username_2, great answer! But I'm gonna give this one to username_1, since he touched upon cache invalidation as well.  
  > **username_2**: I would have given it to username_1 too  
  > **username_0**: Lol! I still very much appreciate the answer. :)  
  
  ---
  
  ### Answer by username_3 on 2017-12-06. Score: 0
  
  More than you ever wanted to know about the Craft `{% cache %}` tag:
  
  [The Craft {% cache %} Tag In-Depth](https://nystudio107.com/blog/the-craft-cache-tag-in-depth)
  
  
  ---
  
  