<p>When working with multiple environments you likely want to disable caching for all but one  environment, because Craft saves the absolute url to entries/assets in the cache.</p>

<p>I achieved this by setting a custom variable in <code>general.php</code></p>

<pre><code>'craft.dev' =&gt; array(
    'cache' =&gt; true,
    'environmentVariables' =&gt; array(...),
)

 '192.168.178.20' =&gt; array(
    'cache' =&gt; false,
    'environmentVariables' =&gt; array(...),
)
</code></pre>

<p>and then add a conditional to the cache function in the template</p>

<pre><code>{% cache unless not craft.config.cache %}
    {# ... #}
{% endcache %}
</code></pre>

<p>With this setup I can now access the cached content on <code>craft.dev</code> and open the site on another devices in the network on <code>192.168.178.20</code> without having problems with cached urls.</p>

CC BY-SA 3.0
2014-06-13T16:51:07.097Z
Brilliant, nice work!
Jason McCallister
CC BY-SA 3.0
2016-02-23T09:56:16.133Z
Isn't `'cache' => true` a invalid configuration property as you mentioned in one of my questions.
KSPR
CC BY-SA 3.0
2014-06-12T22:54:00.260Z
2014-06-12T23:01:19.493Z
2014-06-12T23:01:19.493Z
carlcs
<p>Generally you’ll just want to cache areas of your templates that are going to trigger a lot of database queries, or fetch a lot of data, since those are going to have the biggest hit on your site’s performance. Matrix field loops that have relationship fields inside them (Entries/Assets/etc.) are a good example, since each Matrix block is going to come with one or more DB queries.</p>

<p>There’s not much advantage to caching less intensive areas of your templates, and might lead to unexpected consequences, if you have anything dynamic being output that’s not pure-content, such as the logged in user’s username.</p>

<blockquote>
  <p>..to combine the cache-tag with other <a href="https://buildwithcraft.com/docs/config-settings#cacheMethod">supported caches</a>?</p>
</blockquote>

<p>Worth mentioning that template caching does <em>not</em> respect the cacheMethod setting – that’s just for data caches. Template caches always go to the database, which in this case is the most performant option.</p>

CC BY-SA 3.0
2014-06-12T23:40:55.447Z
Thanks for the explanation. One more question: Why do the template caches go to the database? There are plugins for other CMS's which cache templates as .html files. Wouldn't sth. similar for craft enhance performance even more?
Victor
CC BY-SA 3.0
2014-06-12T23:43:20.600Z
Saving to the database provides the most efficient cache-busting logic. We save all caches in the `templatecaches` table, and a list of elements that are used in each cache in the `templatecacheelements` table, making it really easy to identify which caches should be deleted when you update/delete an element.
Brandon Kelly
CC BY-SA 3.0
2014-06-12T23:29:45.133Z
2014-06-12T23:29:45.133Z
Brandon Kelly
<p>I started using the <a href="https://buildwithcraft.com/docs/templating/tags#cache">{% cache %} tag</a> and would like to know what the best practices are:</p>

<p>.</p>

<p><strong>Regarding performance:</strong></p>

<p>For example does it make sense..</p>

<ul>
<li>..to cache the whole template, or is it better to enclose each query separately?</li>
<li>..to combine the cache-tag with other <a href="https://buildwithcraft.com/docs/config-settings#cacheMethod">supported caches</a>?</li>
<li>..to cache non-dynamic content?</li>
</ul>

<p>.</p>

<p><strong>Regarding development:</strong></p>

<p>I saw the tip to use: <code>{% cache unless craft.config.devMode﻿ %}</code>. Is there anything else to consider during development?</p>

<performance><cache>
What are the best practices for using the {% cache %} tag?
# What are the best practices for using the {% cache %} tag?

(Asked by: username_0 on 2014-06-12)

I started using the [{% cache %} tag](https://buildwithcraft.com/docs/templating/tags#cache) and would like to know what the best practices are:

.

**Regarding performance:**

For example does it make sense..

* ..to cache the whole template, or is it better to enclose each query separately?
* ..to combine the cache-tag with other [supported caches](https://buildwithcraft.com/docs/config-settings#cacheMethod)?
* ..to cache non-dynamic content?

.

**Regarding development:**

I saw the tip to use: `{% cache unless craft.config.devMode﻿ %}`. Is there anything else to consider during development?

**Tags:** performance, cache

---

## 2 answers

### Answer by username_4 on 2014-06-12. Score: 39 | Accepted answer

Generally you’ll just want to cache areas of your templates that are going to trigger a lot of database queries, or fetch a lot of data, since those are going to have the biggest hit on your site’s performance. Matrix field loops that have relationship fields inside them (Entries/Assets/etc.) are a good example, since each Matrix block is going to come with one or more DB queries.

There’s not much advantage to caching less intensive areas of your templates, and might lead to unexpected consequences, if you have anything dynamic being output that’s not pure-content, such as the logged in user’s username.

> ..to combine the cache-tag with other [supported caches](https://buildwithcraft.com/docs/config-settings#cacheMethod)?

Worth mentioning that template caching does *not* respect the cacheMethod setting – that’s just for data caches. Template caches always go to the database, which in this case is the most performant option.

> **Comments:**
> **username_0**: Thanks for the explanation. One more question: Why do the template caches go to the database? There are plugins for other CMS's which cache templates as .html files. Wouldn't sth. similar for craft enhance performance even more?  
> **username_4**: Saving to the database provides the most efficient cache-busting logic. We save all caches in the `templatecaches` table, and a list of elements that are used in each cache in the `templatecacheelements` table, making it really easy to identify which caches should be deleted when you update/delete an element.  

---

### Answer by username_1 on 2014-06-12. Score: 32

When working with multiple environments you likely want to disable caching for all but one  environment, because Craft saves the absolute url to entries/assets in the cache.
I achieved this by setting a custom variable in general.php
'craft.dev' => array(
    'cache' => true,
    'environmentVariables' => array(...),
)

 '192.168.178.20' => array(
    'cache' => false,
    'environmentVariables' => array(...),
)

and then add a conditional to the cache function in the template
{% cache unless not craft.config.cache %}
    {# ... #}
{% endcache %}

With this setup I can now access the cached content on craft.dev and open the site on another devices in the network on 192.168.178.20 without having problems with cached urls.

> **Comments:**
> **username_2**: Brilliant, nice work!  
> **username_3**: Isn't `'cache' => true` a invalid configuration property as you mentioned in one of my questions.  

---

