I have a question about the {% cache %} tag with Craft 3. I am reading this article (https://nystudio107.com/blog/the-craft-cache-tag-in-depth), and trying out implementing the tags; however, when I check the amount of db queries before and after adding the tags, the amount of queries goes up instead of down as suggested in the article.

What would cause this to happen? This is the code I’m currently testing the cache tag on, there are 5000+ entires in the 'articles' section. Without the cache tag there are 19 database queries, with it there are 32 queries.

{% cache %}
    {% for entry in craft.entries.section('articles').all() %}
      {{ entry.title }}<br>
    {% endfor %}
{% endcache %}
  • When you're checking the database queries, I'm assuming that's on first load, when there is currently no data cached? Do subsequent loads have a smaller number of queries?
    – Brad Bell
    Oct 27 '18 at 21:27
  • 1
    Hi Brad, thanks for the reply, that was the issue! The next few loads had 17 queries. Oct 31 '18 at 14:16

When you first load the template using the {% cache %} tags, the number of queries will likely be higher because it still has to retrieve the correct data from the database.

Subsequent loads of that template should show a lower amount of queries because it's returning actual cached content at that point.

The same thing will happen once the cache is busted from expiration on the next template load as well.

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.