Skip to content

Commit 7c1ac5e

Browse files
committed
Merge branch '5.1'
* 5.1: Improved the HTTPCache examples mentioning s-maxage
2 parents 0494bc4 + 16060ad commit 7c1ac5e

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

http_cache.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ The *easiest* way to cache a response is by caching it for a specific amount of
244244
// somehow create a Response object, like by rendering a template
245245
$response = $this->render('blog/index.html.twig', []);
246246

247-
// cache for 3600 seconds
248-
$response->setSharedMaxAge(3600);
247+
// cache publicly for 3600 seconds
248+
$response->setPublic();
249+
$response->setMaxAge(3600);
249250

250251
// (optional) set a custom Cache-Control directive
251252
$response->headers->addCacheControlDirective('must-revalidate', true);
@@ -257,7 +258,7 @@ Thanks to this new code, your HTTP response will have the following header:
257258

258259
.. code-block:: text
259260
260-
Cache-Control: public, s-maxage=3600, must-revalidate
261+
Cache-Control: public, maxage=3600, must-revalidate
261262
262263
This tells your HTTP reverse proxy to cache this response for 3600 seconds. If *anyone*
263264
requests this URL again before 3600 seconds, your application *won't* be hit at all.

http_cache/esi.rst

+12-14
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,15 @@ independently of the rest of the page::
105105
public function about()
106106
{
107107
$response = $this->render('static/about.html.twig');
108-
// sets the shared max age - which also marks the response as public
109-
$response->setSharedMaxAge(600);
108+
$response->setPublic();
109+
$response->setMaxAge(600);
110110

111111
return $response;
112112
}
113113
}
114114

115-
In this example, the full-page cache has a lifetime of ten minutes.
115+
In this example, the response is marked as public to make the full page
116+
cacheable for all requests with a lifetime of ten minutes.
116117
Next, include the news ticker in the template by embedding an action.
117118
This is done via the ``render()`` helper (for more details, see how to
118119
:ref:`embed controllers in templates <templates-embed-controllers>`).
@@ -170,14 +171,19 @@ of the master page::
170171
public function latest($maxPerPage)
171172
{
172173
// ...
173-
$response->setSharedMaxAge(60);
174+
$response->setPublic();
175+
$response->setMaxAge(60);
174176

175177
return $response;
176178
}
177179
}
178180

179-
With ESI, the full page cache will be valid for 600 seconds, but the news
180-
component cache will only last for 60 seconds.
181+
In this example, the embedded action is cached publicly too because the contents
182+
are the same for all requests. However, in other cases you may need to make this
183+
response non-public and even non-cacheable, depending on your needs.
184+
185+
Putting all the above code together, with ESI the full page cache will be valid
186+
for 600 seconds, but the news component cache will only last for 60 seconds.
181187

182188
.. _http_cache-fragments:
183189

@@ -233,14 +239,6 @@ possible.
233239
signed when using the fragment renderer and the ``render_esi`` Twig
234240
function.
235241

236-
.. note::
237-
238-
Once you start using ESI, remember to always use the ``s-maxage``
239-
directive instead of ``max-age``. As the browser only ever receives the
240-
aggregated resource, it is not aware of the sub-components, and so it will
241-
obey the ``max-age`` directive and cache the entire page. And you don't
242-
want that.
243-
244242
The ``render_esi`` helper supports two other useful options:
245243

246244
``alt``

http_cache/expiration.rst

+17-6
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,24 @@ is used to specify many different cache directives::
2626

2727
// sets the number of seconds after which the response
2828
// should no longer be considered fresh by shared caches
29-
$response->setSharedMaxAge(600);
29+
$response->setPublic();
30+
$response->setMaxAge(600);
3031

3132
The ``Cache-Control`` header would take on the following format (it may have
3233
additional directives):
3334

3435
.. code-block:: text
3536
36-
Cache-Control: public, s-maxage=600
37+
Cache-Control: public, maxage=600
38+
39+
.. note::
40+
41+
Using the ``setSharedMaxAge()`` method is not equivalent to using both
42+
``setPublic()`` and ``setMaxAge()`` methods. According to the
43+
`Serving Stale Responses`_ section of RFC 7234, the ``s-maxage`` setting
44+
(added by ``setSharedMaxAge()`` method) prohibits a cache to use a stale
45+
response in ``stale-if-error`` scenarios. That's why it's recommended to use
46+
both ``public`` and ``max-age`` directives.
3747

3848
.. index::
3949
single: Cache; Expires header
@@ -76,9 +86,10 @@ servers should not send ``Expires`` dates more than one year in the future."
7686

7787
.. note::
7888

79-
According to `RFC 7234 - Caching`_, the ``Expires`` header value is ignored
80-
when the ``s-maxage`` or ``max-age`` directive of the ``Cache-Control``
81-
header is defined.
89+
According to the `Calculating Freshness Lifetime`_ section of RFC 7234,
90+
the ``Expires`` header value is ignored when the ``s-maxage`` or ``max-age``
91+
directive of the ``Cache-Control`` header is defined.
8292

8393
.. _`expiration model`: https://tools.ietf.org/html/rfc2616#section-13.2
84-
.. _`RFC 7234 - Caching`: https://tools.ietf.org/html/rfc7234#section-4.2.1
94+
.. _`Calculating Freshness Lifetime`: https://tools.ietf.org/html/rfc7234#section-4.2.1
95+
.. _`Serving Stale Responses`: https://tools.ietf.org/html/rfc7234#section-4.2.4

0 commit comments

Comments
 (0)