@@ -17,7 +17,7 @@ Symfony2 cache system.
17
17
.. index ::
18
18
single: Cache; Types of
19
19
single: Cache; Proxy
20
- single: Cache; Reverse proxy
20
+ single: Cache; Reverse Proxy
21
21
single: Cache; Gateway
22
22
23
23
Kinds of Caches
@@ -45,7 +45,7 @@ caches:
45
45
46
46
HTTP 1.1 allows caching anything by default unless there is an explicit
47
47
``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
49
49
when responses have a redirect status code.
50
50
51
51
When a request has a cookie, PHP automatically adds the ``private `` directive
@@ -121,7 +121,7 @@ headers easily::
121
121
Understanding HTTP Cache
122
122
------------------------
123
123
124
- The HTTP specification (aka `RFC2616 `_) defines two caching models:
124
+ The HTTP specification (aka `RFC 2616 `_) defines two caching models:
125
125
126
126
* *Expiration *: You specify how long a response should be considered "fresh"
127
127
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.
137
137
138
138
.. tip ::
139
139
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
141
141
not describe a new version of HTTP, but mostly clarifies the original HTTP
142
142
specification. The organization is also much better as the specification
143
143
is split into several parts; everything related to HTTP caching can be
@@ -169,7 +169,7 @@ scaling).
169
169
Expiration with the ``Expires `` Header
170
170
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171
171
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
173
173
which the response is considered stale." The ``Expires `` header can be set
174
174
with the ``setExpires() `` Response method. It takes a ``DateTime `` instance as
175
175
an argument::
@@ -398,6 +398,58 @@ environment, use it to debug and validate your cache strategy::
398
398
399
399
error_log($kernel->getLog());
400
400
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
+
401
453
The Symfony2 reverse proxy is a great tool to use when developing your website
402
454
on your local network or when you deploy your website on a shared host where
403
455
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.
609
661
.. index ::
610
662
single: Cache; Invalidation with Varnish
611
663
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:
614
688
615
689
.. code-block :: text
616
690
@@ -632,9 +706,9 @@ you use Varnish, it's a matter of configuring it properly:
632
706
You must protect the ``PURGE `` HTTP method somehow to avoid random people
633
707
purging your cached data.
634
708
635
- .. _`RFC2616 ` : http://www.ietf.org/rfc/rfc2616.txt
709
+ .. _`RFC 2616 ` : http://www.ietf.org/rfc/rfc2616.txt
636
710
.. _`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
639
713
.. _`ESI` : http://www.w3.org/TR/esi-lang
640
714
.. _`Edge Architecture` : http://www.w3.org/TR/edge-arch
0 commit comments