# Cache a template fragment

Source: https://zotonic.com/docs/2946/cache-a-template-fragment

URI: https://zotonic.com/id/2946

Language: en

First measure the work performed by the fragment. Cache a fragment only when you know which inputs and data changes affect its output.

Check <a href="/id/1357" class="doc-reference doc-reference-tag"><code>tag#cache</code></a> for the supported duration, variation, and dependency arguments. A fragment containing a user's private information must not be shared between users through an incomplete cache key. Language and resource identity can also change the result.

Test a cache hit, then change the underlying content and check invalidation. During debugging, the Development page can disable template cache tags. Restore that setting before measuring normal page behavior. See [Distinguish stale cache data from stale code](/id/2979) and [Choose checks for a change](/id/2983).

For a public resource card, a starting point is:

```django
{% cache 60 garden_card vary=id vary=z_language if_anonymous %}
    <a href="{{ id.page_url }}">{{ id.title }}</a>
{% endcache %}
```

The resource ID varies the entry and is a cache dependency; language separates translations. This example caches only anonymous requests. If the fragment also reads connected resources, add the corresponding dependencies. Rename a shared cache block when its output contract changes.

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2946?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2946?language=en>)
