Invalidate cache on new order I have a list of orders from Commerce 1 (Craft 2) in the frontend.

I want to invalidate the cache when a new order is placed but it doesn't work:

{% set latestOrder = craft.commerce.orders.first() %}

{% cache until latestOrder.dateOrdered  %}
   Last Updated: {{ orders.dateOrdered.first() | date('D d.m.Y - H:i') }}
   {# stuff #}
{% endcache %}


The Last Updated tag shows always the latest browser reload. What's the error here?
In your code you are only caching until the dateOrdered of the latest order. That means that viewing the page will always be ahead of the cache time.

You could look to do the following:

{% set latestOrder = craft.commerce.orders.order('dateOrdered DESC').first() %}
{% set cacheKey = 'orders-' ~ latestOrder.dateOrdered|date('U') %}

{% cache using key cacheKey %}
  {% set orders = craft.commerce.orders.order('dateOrdered DESC').find() %}
  &lt;ul&gt;
    {% for order in orders %}
      &lt;li&gt;{{ order.id }}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
{% endcache %}


This creates a cache key based on the date of the latest order. Therefore, when there is a new order this key will change.