6

What effect, if any, does nesting cache tags have? It seems like it would be redundant, but are there any inherent drawbacks?

Recently, I can across an instance where I was using the {% cache %} tag in an include to cache a large matrix field. This was very successful at reducing querries and speeding things up on several pages that used that same include.

But one template that included this file used more entries tags so it seemed to make sense to wrap all of them in a cache tag as well. Here's an example.

_includes/article-body.html

{% cache %}
  {% for block in entry.bigMatrixField %}
    ...
  {% endfor %}
{% endcache %}

blog/_entry.html

{% cache %}
  {% for entry in craft.entries.section('blog') %}
    ...(some expensive queries)...
  {% endfor %}

  {% include "_includes/article-body" %}

  {% for category in craft.categories.group('blogCategories') %}
    ...(some more expensive queries)...
  {% endfor %}
{% endcache %}
  • It does not really matter, blog/_entry.html will load a cached version of _includes/article_body.html and cache the whole once again. – Bob Olde Hampsink Jun 18 '15 at 17:33
2

Nesting cache tags shouldn't cause any new problems (assuming you've taken into account all the fun problems already caused by cache).

Each cache tag will store it's contents in a database table for easier access. We can illustrate this a bit easier using cache tags with keys. Take the following example with two cache tags using the keys dog and cat

{% cache using key 'dog' %}
    BUSTER
    {% cache using key 'cat' %} 
        KITTEN
    {% endcache %}
{% endcache %}

When the cache tags are run they store the following:

cat stores the value:

KITTEN

dog stores the value:

BUSTER
KITTEN

The spacing in my example above may not be perfectly accurate but the gist of what content is stored should be. In the case that the page is loaded a second time, when the dog cache tag is found, Craft will pull it's content from the database. The cat cache tag will never get run, but it doesn't matter, because the full contents of the wrapping dog cache tag have already been retrieved.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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