4

My understanding of the {% cache %} tag is that if the content between it changes, the cache is invalidated and you see the new content.

I'm having an issue where a new entry is created in section, but it doesn't show up on the front-end unless I manually clear the cache in the control panel.

The content is pulled in via a loop such as:

{%  for entry in craft.entries.section( "news" ).limit(3) %}

Am I not understanding how this tag works or is something going wrong?

Edit: Here is the full code block wrapped by cache tags

{% cache %}
{%  for entry in craft.entries.section( item.nameId ).limit(3) %}
    <div class="cardRow_column-carousel">
        <a href="{{ entry.url }}" class="card" >
            <div class="card_imageHolder">
                <img class="card_image" src="{{ entry.tileImage.first().getUrl(cardCrop) }}">
            </div>
            <div class="card_content">
                <h4 class="card_content_heading">{{ entry.title }}</h4>
                <div class="card_content_icon">
                    {% if item.type == "video" %}
                        {% set iconType = "icon-video" %}
                    {% elseif item.type == "game" %}
                        {% set iconType = "icon-games" %}
                    {% elseif item.type == "image" or item.type == "imageGallery" %}
                        {% set iconType = "icon-image" %}
                    {% else %}
                        {% set iconType = "icon-paper" %}
                    {% endif %}
                    <i class="{{ iconType }} card_content_icon-category"></i>
                    <i class="icon-arrow-right card_content_icon_arrow"></i>
                </div>
            </div>
        </a>
    </div>
{% endfor %}
{% endcache %}

The strange thing that I'm noticing is if I add an entry it never shows up, but if I delete one it does get cleared out. And deleting an entry then causes the new one to show up.

UPDATE: Craft support determined there was a bug and plans to fix it in the next version. Current version is Craft 2.1 build 2563.

  • I've had the same issue as well, basically had to turn off all caching for now until I find a fix – Keith Mancuso Jul 3 '14 at 1:23
  • is the cache tag that wraps this line in the same template? – Luke Holder Jul 3 '14 at 7:20
  • Can you post the portion of the template that is using the cache tag? – Brad Bell Jul 3 '14 at 7:57
  • I posted the full code block above with an additional note about observations deleting entries. – kekona Jul 3 '14 at 17:42
4

When new elements are created, there’s no way for Craft to immediately know which template caches should be cleared. That’s where the Delete Stale Template Caches task comes in. It loops through all of your caches that included element queries, re-runs the queries, and checks to see if the new element is now included in the results.

There are two things to be aware of, though:

  1. Your element query must happen inside the {% cache %} tag pair.

    bad:
    
    {% set entries = craft.entries.section('news').limit(3) %}
    {% cache %}
        {% for entry in entries %}
            ...
        {% endfor %}
    {% endcache %}
    
    good:
    
    {% cache %}
        {% set entries = craft.entries.section('news').limit(3) %}
        {% for entry in entries %}
            ...
        {% endfor %}
    {% endcache %}
    
  2. The cache querying is not immediate. After saving the entry you will see a little progress bar in the CP header. You need to wait until that has completed before your caches will be cleared.

  • I edited my question and posted the code block which is wrapped in the cache tags. I see the progress bar in the header when I save the entry. It completes pretty quickly and the entry still doesn't show up on the front-end, even if I wait for minutes. If I add an expiration time to the cache tag then I will see it show up. – kekona Jul 3 '14 at 17:58
  • In that case you might be running into a bug. Please send us a ticket from your dashboard, including your templates and DB backup. – Brandon Kelly Jul 3 '14 at 18:01
  • @BrandonKelly We are experiencing this same issue currently on a site. Was this confirmed to be a bug? We are running Craft Pro 2.3.2640. – aran Mar 26 '15 at 19:50
  • @aran Sorry for the late reply. If you're still experiencing an issue, please contact support@buildwithcraft.com. – Brandon Kelly Apr 13 '15 at 13:41
  • Sorry for digging this up. I am having a similar situation where the new entry gets created from a front end form. So the Delete Stale Template Caches task won’t get fired (I assume). I can trigger it by moving to the cp and hit reload or navigate around. Is there a way to trigger that task from the front end on entry creation? – Marcus Scheller Aug 21 '16 at 21:36

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.