Skip to content

Improved the HTTPCache examples mentioning s-maxage #13359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions http_cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ The *easiest* way to cache a response is by caching it for a specific amount of
// somehow create a Response object, like by rendering a template
$response = $this->render('blog/index.html.twig', []);

// cache for 3600 seconds
$response->setSharedMaxAge(3600);
// cache publicly for 3600 seconds
$response->setPublic();
$response->setMaxAge(3600);

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

.. code-block:: text

Cache-Control: public, s-maxage=3600, must-revalidate
Cache-Control: public, maxage=3600, must-revalidate

This tells your HTTP reverse proxy to cache this response for 3600 seconds. If *anyone*
requests this URL again before 3600 seconds, your application *won't* be hit at all.
Expand Down
26 changes: 12 additions & 14 deletions http_cache/esi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ independently of the rest of the page::
public function about()
{
$response = $this->render('static/about.html.twig');
// sets the shared max age - which also marks the response as public
$response->setSharedMaxAge(600);
$response->setPublic();
$response->setMaxAge(600);

return $response;
}
}

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

return $response;
}
}

With ESI, the full page cache will be valid for 600 seconds, but the news
component cache will only last for 60 seconds.
In this example, the embedded action is cached publicly too because the contents
are the same for all requests. However, in other cases you may need to make this
response non-public and even non-cacheable, depending on your needs.

Putting all the above code together, with ESI the full page cache will be valid
for 600 seconds, but the news component cache will only last for 60 seconds.

.. _http_cache-fragments:

Expand Down Expand Up @@ -233,14 +239,6 @@ possible.
signed when using the fragment renderer and the ``render_esi`` Twig
function.

.. note::

Once you start using ESI, remember to always use the ``s-maxage``
directive instead of ``max-age``. As the browser only ever receives the
aggregated resource, it is not aware of the sub-components, and so it will
obey the ``max-age`` directive and cache the entire page. And you don't
want that.

The ``render_esi`` helper supports two other useful options:

``alt``
Expand Down
12 changes: 10 additions & 2 deletions http_cache/expiration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ is used to specify many different cache directives::

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

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

.. code-block:: text

Cache-Control: public, s-maxage=600
Cache-Control: public, maxage=600

.. note::

Using the ``setSharedMaxAge()`` may seem equivalent to using both ``setPublic()``
and ``setMaxAge()`` methods. However, their behavior is different in a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion would be to refer to https://tools.ietf.org/html/rfc7234#section-4.2.4:

"However, the setSharedMaxAge() method and the corresponsing s-maxage setting have the additional implication of prohibiting a cache to use a stale response in particular error scenarios. See RFC 7234 Section 4.2.4 for details."

(Does "stale" make sense in the context of this page, is it a term we have used or defined before?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review! I added this while merging the pull request.

``stale-if-error`` scenario and that's why it's recommended to use both
``public`` and ``max-age`` directives.

.. index::
single: Cache; Expires header
Expand Down