2

The Craft docs have a good overview of the {% cache %} tag.

The docs give a couple examples of when not to use the {% cache %} tag. In particular, they give this example:

Don’t use them to cache static text; that will be more expensive than simply outputting the text.

Can someone explain to me the logic behind this statement? How do the two scenarios play out where caching static text is slower than just outputting it?

Additionally, what if you are caching a page with several queries that includes static text? Does it follow that you'll want to cache the queries in and of themselves and let the static text remain uncached?

2

Since the {% cache %} tag solely stores its information in the database, it has a guaranteed minimum of one database call to make to see if what's inside of it has already been cached.

If you're just doing this:

{% cache %}
    Hi. I'm static text.
{% endcache %}

That's quite a bit more overhead than simply:

Hi. I'm static text.

With no real benefit.

  • Does each {% cache %} tag you use query the database one time? Or if you have several {% cache %} tags on the page, will the db only be queried once for all cached content in the db? – Ben Parizek Feb 8 '15 at 23:22
  • Each {% cache %} tag queries the database at least one time. – Brad Bell Feb 8 '15 at 23:23
  • There's another point here, though. Brandon in a related answer says to cache 'just those areas which involve work'. This might lead to more database calls on a page to check cache. It seems that caching static text as well as the dynamic on a page would be a win, no? – narration_sd Feb 9 '15 at 0:42
  • @narration_sd Right - if there is a mixture of static and dynamic content in the cache tags and the dynamic part is expensive, then you might as well include the static along with it. – Brad Bell Feb 9 '15 at 3:08
  • @BradBell, can you provide an example of a scenario where the cache tag needs to query the database more than one time? – Ben Parizek Feb 9 '15 at 3:37

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.