<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>

CC BY-SA 3.0
2015-06-23T17:08:16.530Z
Great, thanks Aaron... That info on cache invalidation was exactly what I was looking for!
Lindsey D
CC BY-SA 3.0
2015-06-23T01:40:22.397Z
2015-06-23T14:01:10.187Z
2015-06-23T14:01:10.187Z
Aaron Berkowitz
<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>

CC BY-SA 3.0
2015-06-23T17:09:22.090Z
Thanks Marion, great answer! But I'm gonna give this one to Aaron, since he touched upon cache invalidation as well.
Lindsey D
CC BY-SA 3.0
2015-06-23T17:12:59.020Z
I would have given it to Aaron too
Marion Newlevant
CC BY-SA 3.0
2015-06-23T17:13:55.183Z
Lol! I still very much appreciate the answer. :)
Lindsey D
CC BY-SA 3.0
2015-06-23T01:42:20.220Z
2015-06-23T01:42:20.220Z
Marion Newlevant
<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>

CC BY-SA 3.0
2017-12-06T20:11:14.157Z
2017-12-06T20:11:14.157Z
andrew.welch
<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>

<plugin-development><templating><cache><elementcriteriamodel><hook>
Template Hook + Caching
# Template Hook + Caching

(Asked by: username_0 on 2015-06-22)

A portion of my template is being cached, using the standard cache tag:
{% cache %}

Within that cached section of the template, I'm using a template hook:
{% hook 'myTemplateHook' %}

Within that template hook, I'm collecting entry data via a normal ElementCriteriaModel:
$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.
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 Aaron... 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 Marion, great answer! But I'm gonna give this one to Aaron, since he touched upon cache invalidation as well.  
> **username_2**: I would have given it to Aaron 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)


---

