<p>Sure. In my experience it's often better to cache individual entries, rather than whole pages, if the entries have a lot of relations to other elements. It's very resource intensive in Craft to figure out for instance if an image or entry relationship exists. And the more relations you have, the more frequent the cache would break. </p>

<p>The project I'm currently working on has a lot of different landing pages with entry items that have a ton of different images and relations. So I have A LOT of this going on for every single entry in a landing page:</p>

<pre><code>{% if article.commentator | length %}
  {% if article.commentator.first().profileImage | length %}
    {% set listImage = article.commentator.first().profileImage.first() %}
    ...
  {% endif %}
{% endif %}
</code></pre>

<p>I found that when I cached the whole page, it would break often, resulting in a serious performance hit. I changed my code to cache individual entries. The difference in load of the page as a whole was small, but the actual impact in percieved performance was huge. In a positive way. :)</p>

<p>I also use the same generated HTML on a lot of different pages, so I also made the caching of these items globally. I create a key that is unique for the different options the block could have. The final cache tag looks something like this:</p>

<pre><code>{% cache globally using key ("article-grid-item-" ~ article.id ~ "-" ~ gridType ~ "-" ~ modifier ~ "-" ~ craft.config.environment) for 3 hours unless craft.config.templateCacheDisabled %}
...
{% endcache %}
</code></pre>

<p>But the only way to figure out which would be better is to test it on your project. All builds are different, and as far as caching is concerned, there are no simple answers. ;)</p>

<p>And... I also wish P&amp;T would make this easier/less resource consuming: <code>{% if entry.image | length %}</code> :)</p>

CC BY-SA 3.0
2014-12-10T12:31:24.030Z
And can you cache the whole page with all the small entry caches?

Like wrapping a cache around the entry caches?
Johannes Lamers
CC BY-SA 3.0
2014-12-10T13:00:13.700Z
Yes, as far as I can tell, it works just fine to wrap everything in a cache tag for the whole list. I chose not to do it on the project I'm working on, and I think that was because I tested and found that the "small" caches broke when the "big" one did. Kinda ruining the point. BUT, I've tested it again now and that doesn't seem to be the case, it works as it should. Don't know if I did something wrong, or if this is something that was fixed along the way.
André Elvan
CC BY-SA 3.0
2014-12-10T11:40:12.443Z
2014-12-10T11:40:12.443Z
André Elvan
<p>I'm working on a website for a music / artist agency.
On the homepage we need al the artist entries. More than 200. </p>

<p>Each artist profile has an image, an image color extruder picker, some tags, relation, etc etc.</p>

<p>Without caching I have a lot of queries. But the page can render. 
If I enable cache, the server runs out of memory sometimes. And when I enable an entry, or edit an entry, the whole cache with the artist loop gets cleared. </p>

<p><strong>This is my code.</strong> </p>

<pre><code>{% block content %}
    {% cache for 1 week %}
        {% set cases = craft.entries({
            section: 'artist',
            order: 'title',
            limit: null
        }) %}

                {% for entry in cases %}
                    &lt;article class="artist filter-{{ entry.title|first|lower }}"&gt;
                        {% set tags = entry.genre %}
                        {% set agent = entry.agent %}
                        {% set image = entry.introFoto.first() %}

                        ...
                        ... 
                        etc etc


                    &lt;/article&gt;
                {% endfor %}
    {% endcache %}
{% endblock %}
</code></pre>

<p><strong>So can I cache individual entries? Like so?</strong> When someone edits an entry, only that entry cache get cleared?</p>

<pre><code>{% block content %}

        {% set cases = craft.entries({
            section: 'artist',
            order: 'title',
            limit: null
        }) %}

            {% for entry in cases %}
                {% cache for 1 week %}
                    &lt;article class="artist filter-{{ entry.title|first|lower}}"&gt;
                        {% set tags = entry.genre %}
                        {% set agent = entry.agent %}
                        {% set image = entry.introFoto.first() %}

                        ...
                        ... 
                        etc etc


                    &lt;/article&gt;
                {% endcache %}
            {% endfor %}

{% endblock %}
</code></pre>

<cache><performance>
Caching and Memory problems. What is best caching method
# Caching and Memory problems. What is best caching method

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

I'm working on a website for a music / artist agency.
On the homepage we need al the artist entries. More than 200. 
Each artist profile has an image, an image color extruder picker, some tags, relation, etc etc.
Without caching I have a lot of queries. But the page can render. 
If I enable cache, the server runs out of memory sometimes. And when I enable an entry, or edit an entry, the whole cache with the artist loop gets cleared. 
This is my code. 
{% block content %}
    {% cache for 1 week %}
        {% set cases = craft.entries({
            section: 'artist',
            order: 'title',
            limit: null
        }) %}

                {% for entry in cases %}
                    
                        {% set tags = entry.genre %}
                        {% set agent = entry.agent %}
                        {% set image = entry.introFoto.first() %}

                        ...
                        ... 
                        etc etc


                    
                {% endfor %}
    {% endcache %}
{% endblock %}

So can I cache individual entries? Like so? When someone edits an entry, only that entry cache get cleared?
{% block content %}

        {% set cases = craft.entries({
            section: 'artist',
            order: 'title',
            limit: null
        }) %}

            {% for entry in cases %}
                {% cache for 1 week %}
                    
                        {% set tags = entry.genre %}
                        {% set agent = entry.agent %}
                        {% set image = entry.introFoto.first() %}

                        ...
                        ... 
                        etc etc


                    
                {% endcache %}
            {% endfor %}

{% endblock %}

**Tags:** cache, performance


---

## 1 answers

### Answer by username_1 on 2014-12-10. Score: 4 | Accepted answer

Sure. In my experience it's often better to cache individual entries, rather than whole pages, if the entries have a lot of relations to other elements. It's very resource intensive in Craft to figure out for instance if an image or entry relationship exists. And the more relations you have, the more frequent the cache would break. 
The project I'm currently working on has a lot of different landing pages with entry items that have a ton of different images and relations. So I have A LOT of this going on for every single entry in a landing page:
{% if article.commentator | length %}
  {% if article.commentator.first().profileImage | length %}
    {% set listImage = article.commentator.first().profileImage.first() %}
    ...
  {% endif %}
{% endif %}

I found that when I cached the whole page, it would break often, resulting in a serious performance hit. I changed my code to cache individual entries. The difference in load of the page as a whole was small, but the actual impact in percieved performance was huge. In a positive way. :)
I also use the same generated HTML on a lot of different pages, so I also made the caching of these items globally. I create a key that is unique for the different options the block could have. The final cache tag looks something like this:
{% cache globally using key ("article-grid-item-" ~ article.id ~ "-" ~ gridType ~ "-" ~ modifier ~ "-" ~ craft.config.environment) for 3 hours unless craft.config.templateCacheDisabled %}
...
{% endcache %}

But the only way to figure out which would be better is to test it on your project. All builds are different, and as far as caching is concerned, there are no simple answers. ;)
And... I also wish P&T would make this easier/less resource consuming: {% if entry.image | length %} :)

> **Comments:**
> **username_0**: And can you cache the whole page with all the small entry caches?  Like wrapping a cache around the entry caches?  
> **username_1**: Yes, as far as I can tell, it works just fine to wrap everything in a cache tag for the whole list. I chose not to do it on the project I'm working on, and I think that was because I tested and found that the "small" caches broke when the "big" one did. Kinda ruining the point. BUT, I've tested it again now and that doesn't seem to be the case, it works as it should. Don't know if I did something wrong, or if this is something that was fixed along the way.  

---

