Skip to content

Commit e6443bc

Browse files
committed
added more information in the HTTP cache chapter
1 parent c6736da commit e6443bc

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

guides/cache/http.rst

+84-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Symfony2 cache system.
1717
.. index::
1818
single: Cache; Types of
1919
single: Cache; Proxy
20-
single: Cache; Reverse proxy
20+
single: Cache; Reverse Proxy
2121
single: Cache; Gateway
2222

2323
Kinds of Caches
@@ -45,7 +45,7 @@ caches:
4545

4646
HTTP 1.1 allows caching anything by default unless there is an explicit
4747
``Cache-Control`` header. In practice, most caches do nothing when requests
48-
have a cookie, an authentication header, or come with a non-safe method, and
48+
have a cookie, an authorization header, or come with a non-safe method, and
4949
when responses have a redirect status code.
5050

5151
When a request has a cookie, PHP automatically adds the ``private`` directive
@@ -121,7 +121,7 @@ headers easily::
121121
Understanding HTTP Cache
122122
------------------------
123123

124-
The HTTP specification (aka `RFC2616`_) defines two caching models:
124+
The HTTP specification (aka `RFC 2616`_) defines two caching models:
125125

126126
* *Expiration*: You specify how long a response should be considered "fresh"
127127
by including a ``Cache-Control`` and/or an ``Expires`` header. Caches that
@@ -137,7 +137,7 @@ The goal of both models is to never generate the same Response twice.
137137

138138
.. tip::
139139

140-
There is an on-going effort (`HTTP Bis`_) to rewrite the RFC2616. It does
140+
There is an on-going effort (`HTTP Bis`_) to rewrite the RFC 2616. It does
141141
not describe a new version of HTTP, but mostly clarifies the original HTTP
142142
specification. The organization is also much better as the specification
143143
is split into several parts; everything related to HTTP caching can be
@@ -169,7 +169,7 @@ scaling).
169169
Expiration with the ``Expires`` Header
170170
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171171

172-
According to RFC2616, "the ``Expires`` header field gives the date/time after
172+
According to RFC 2616, "the ``Expires`` header field gives the date/time after
173173
which the response is considered stale." The ``Expires`` header can be set
174174
with the ``setExpires()`` Response method. It takes a ``DateTime`` instance as
175175
an argument::
@@ -398,6 +398,58 @@ environment, use it to debug and validate your cache strategy::
398398

399399
error_log($kernel->getLog());
400400

401+
The ``AppCache`` object has a sensible default configuration, but it can be
402+
finely tuned via a set of options you can set by overriding the
403+
``getOptions()`` method::
404+
405+
// app/AppCache.php
406+
class BlogCache extends Cache
407+
{
408+
protected function getOptions()
409+
{
410+
return array(
411+
'debug' => false,
412+
'default_ttl' => 0,
413+
'private_headers' => array('Authorization', 'Cookie'),
414+
'allow_reload' => false,
415+
'allow_revalidate' => false,
416+
'stale_while_revalidate' => 2,
417+
'stale_if_error' => 60,
418+
);
419+
}
420+
}
421+
422+
Here is a list of the main options:
423+
424+
* ``default_ttl``: The number of seconds that a cache entry should be
425+
considered fresh when no explicit freshness information is provided in a
426+
response. Explicit ``Cache-Control`` or ``Expires`` headers override this
427+
value (default: ``0``);
428+
429+
* ``private_headers``: Set of request headers that trigger "private"
430+
``Cache-Control`` behavior on responses that don't explicitly state whether
431+
the response is ``public`` or ``private`` via a ``Cache-Control`` directive.
432+
(default: ``Authorization`` and ``Cookie``);
433+
434+
* ``allow_reload``: Specifies whether the client can force a cache reload by
435+
including a ``Cache-Control`` "no-cache" directive in the request. Set it to
436+
``true`` for compliance with RFC 2616 (default: ``false``);
437+
438+
* ``allow_revalidate``: Specifies whether the client can force a cache
439+
revalidate by including a ``Cache-Control`` "max-age=0" directive in the
440+
request. Set it to ``true`` for compliance with RFC 2616 (default: false);
441+
442+
* ``stale_while_revalidate``: Specifies the default number of seconds (the
443+
granularity is the second as the Response TTL precision is a second) during
444+
which the cache can immediately return a stale response while it revalidates
445+
it in the background (default: ``2``); this setting is overridden by the
446+
``stale-while-revalidate`` HTTP ``Cache-Control`` extension (see RFC 5861);
447+
448+
* ``stale_if_error``: Specifies the default number of seconds (the granularity
449+
is the second) during which the cache can serve a stale response when an
450+
error is encountered (default: ``60``). This setting is overridden by the
451+
``stale-if-error`` HTTP ``Cache-Control`` extension (see RFC 5861).
452+
401453
The Symfony2 reverse proxy is a great tool to use when developing your website
402454
on your local network or when you deploy your website on a shared host where
403455
you cannot install anything beyond PHP code. But being written in PHP, it
@@ -609,8 +661,30 @@ cache for a given URL by requesting it with the special ``PURGE`` HTTP method.
609661
.. index::
610662
single: Cache; Invalidation with Varnish
611663

612-
The Symfony2 reverse proxy supports the ``PURGE`` HTTP method natively, and if
613-
you use Varnish, it's a matter of configuring it properly:
664+
Here is how you can configure the Symfony2 reverse proxy to support the
665+
``PURGE`` HTTP method::
666+
667+
// app/AppCache.php
668+
class AppCache extends Cache
669+
{
670+
protected function lookup(Request $request)
671+
{
672+
if ('PURGE' !== $request->getMethod()) {
673+
return parent::lookup($request);
674+
}
675+
676+
$response = new Response();
677+
if (!$this->store->purge($request->getUri())) {
678+
$response->setStatusCode(404, 'Not purged');
679+
} else {
680+
$response->setStatusCode(200, 'Purged');
681+
}
682+
683+
return $response;
684+
}
685+
}
686+
687+
And the same can be done with Varnish too:
614688

615689
.. code-block:: text
616690
@@ -632,9 +706,9 @@ you use Varnish, it's a matter of configuring it properly:
632706
You must protect the ``PURGE`` HTTP method somehow to avoid random people
633707
purging your cached data.
634708

635-
.. _`RFC2616`: http://www.ietf.org/rfc/rfc2616.txt
709+
.. _`RFC 2616`: http://www.ietf.org/rfc/rfc2616.txt
636710
.. _`HTTP Bis`: http://tools.ietf.org/wg/httpbis/
637-
.. _`P4 - Conditional Requests`:
638-
.. _`P6 - Caching: Browser and intermediary caches`:
711+
.. _`P4 - Conditional Requests`: http://tools.ietf.org/id/draft-ietf-httpbis-p4-conditional-12.txt
712+
.. _`P6 - Caching: Browser and intermediary caches`: http://tools.ietf.org/id/draft-ietf-httpbis-p6-cache-12.txt
639713
.. _`ESI`: http://www.w3.org/TR/esi-lang
640714
.. _`Edge Architecture`: http://www.w3.org/TR/edge-arch

0 commit comments

Comments
 (0)