Template Caching

For now we support just template block caching with the cache block.

The Cache block is simple, and accepts only two parameters: cache key and strategy object (with strategy key and value).

Note

Cache blocks can be nested:

1
2
3
4
5
6
7
{% cache 'v1' {time: 900} %}
    {% for item in items %}
        {% cache 'v1' {gen: item} %}
            {# ... #}
        {% endcache %}
    {% endfor %}
{% endcache %}

The annotation can also be an expression:

1
2
3
4
{% set version = 42 %}
{% cache 'hello_v' ~ version {time: 300} %}
    Hello {{ name }}!
{% endcache %}

There is no need to invalidate keys - the system will clear unused cache entries automatically.

Strategies

There are two available cache strategies: lifetime and generational.

With lifetime as a strategy key you need to provide time with a value in seconds.

1
2
3
4
{# delegate to lifetime strategy #}
{% cache 'v1/summary' {time: 300} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

With generational as a strategy key you need to provide gen with object or array as the value.

1
2
3
4
{# delegate to generational strategy #}
{% cache 'v1/summary' {gen: gimme.article} %}
    {# heavy lifting template stuff here, include/render other partials etc #}
{% endcache %}

Note

You can pass Meta object to generational strategy and it will be used for key generation. If Meta value have created_at or updated_at then those properties will be used, otherwise key will be generated only from object id.