blob: b1d032bc461dff8979c952f690260a1e9b4227f1 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``cache``
2=========
3
4.. versionadded:: 3.2
5
6 The ``cache`` tag was added in Twig 3.2.
7
8The ``cache`` tag tells Twig to cache a template fragment:
9
10.. code-block:: twig
11
12 {% cache "cache key" %}
13 Cached forever (depending on the cache implementation)
14 {% endcache %}
15
16If you want to expire the cache after a certain amount of time, specify an
17expiration in seconds via the ``ttl()`` modifier:
18
19.. code-block:: twig
20
21 {% cache "cache key" ttl(300) %}
22 Cached for 300 seconds
23 {% endcache %}
24
25The cache key can be any string that does not use the following reserved
26characters ``{}()/\@:``; a good practice is to embed some useful information in
27the key that allows the cache to automatically expire when it must be
28refreshed:
29
30* Give each cache a unique name and namespace it like your templates;
31
32* Embed an integer that you increment whenever the template code changes (to
33 automatically invalidate all current caches);
34
35* Embed a unique key that is updated whenever the variables used in the
36 template code changes.
37
38For instance, I would use ``{% cache "blog_post;v1;" ~ post.id ~ ";" ~
39post.updated_at %}`` to cache a blog content template fragment where
40``blog_post`` describes the template fragment, ``v1`` represents the first
41version of the template code, ``post.id`` represent the id of the blog post,
42and ``post.updated_at`` returns a timestamp that represents the time where the
43blog post was last modified.
44
45Using such a strategy for naming cache keys allows to avoid using a ``ttl``.
46It's like using a "validation" strategy instead of an "expiration" strategy as
47we do for HTTP caches.
48
49If your cache implementation supports tags, you can also tag your cache items:
50
51.. code-block:: twig
52
53 {% cache "cache key" tags('blog') %}
54 Some code
55 {% endcache %}
56
57 {% cache "cache key" tags(['cms', 'blog']) %}
58 Some code
59 {% endcache %}
60
61The ``cache`` tag creates a new "scope" for variables, meaning that the changes
62are local to the template fragment:
63
64.. code-block:: twig
65
66 {% set count = 1 %}
67
68 {% cache "cache key" tags('blog') %}
69 {# Won't affect the value of count outside of the cache tag #}
70 {% set count = 2 %}
71 Some code
72 {% endcache %}
73
74 {# Displays 1 #}
75 {{ count }}
76
77.. note::
78
79 The ``cache`` tag is part of the ``CacheExtension`` which is not installed
80 by default. Install it first:
81
82 .. code-block:: bash
83
84 $ composer require twig/cache-extra
85
86 On Symfony projects, you can automatically enable it by installing the
87 ``twig/extra-bundle``:
88
89 .. code-block:: bash
90
91 $ composer require twig/extra-bundle
92
93 Or add the extension explicitly on the Twig environment::
94
95 use Twig\Extra\Cache\CacheExtension;
96
97 $twig = new \Twig\Environment(...);
98 $twig->addExtension(new CacheExtension());
99
100 If you are not using Symfony, you must also register the extension runtime::
101
102 use Symfony\Component\Cache\Adapter\FilesystemAdapter;
103 use Symfony\Component\Cache\Adapter\TagAwareAdapter;
104 use Twig\Extra\Cache\CacheRuntime;
105 use Twig\RuntimeLoader\RuntimeLoaderInterface;
106
107 $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
108 public function load($class) {
109 if (CacheRuntime::class === $class) {
110 return new CacheRuntime(new TagAwareAdapter(new FilesystemAdapter()));
111 }
112 }
113 });