cache

Cache a frequently used block for later reuse.

Cache the output of the enclosed block. The block can be named. Cache blocks with the same name will use each others cached entries, when the cache entry is still valid.

Example:

{% cache 3600 now %}Local time is {% now "Y-m-d H:i:s" %}{% endcache %}

This caches the output of the block for an hour. The name for the cache is “now”.

The cache duration and name are optional. The default cache duration is 0 (zero) seconds, which gives parallel rendering protection (see below) though will not store the result in the cache.

The cache tag protects against the situation where parallel requests need to render the same block at the same time. Instead of all the processes rendering the block in parallel, one process will render the block and share the result with the other—waiting—processes.

Example, prevent parallel rendering (slam dunk) by non logged on visitors:

{% cache if_anonymous %} insert complicated page here {% endcache %}

Besides the duration and the cache name the {% cache %} tag also accepts the following arguments:

Argument Description Example
vary This argument can be used multiple times. Cache blocks with different vary arguments have different cache keys. The arguments are assumed to be keys in the cache. When one of the vary keys is updated or invalidated then the cached block will be invalidated. vary=myid
cat Category the cached block depends on. This argument can be used multiple times for specifying multiple categories. The categories are not added to the cache key, only added as cache dependencies. cat=”news”
if Only cache the block when the argument evaluates to true. if=can_cache
if_anonymous Only cache the block when the current visitor is not logged on (i.e. an anonymous visitor) if_anonymous
visible_for Sets the access control user for rendering the block. With this you can force to only show public items for logged on users. Valid values are “user”, 3, “group”, 2, “community”, 1, “world”, “public”, 0 visible_for=”world”

The cache tag can be disabled by setting the config key mod_development.nocache. This can be done on the /admin/development page.

Edit on GitHub