@@ -328,6 +328,13 @@ its creation more manageable::
328328 // set a custom Cache-Control directive
329329 $response->headers->addCacheControlDirective('must-revalidate', true);
330330
331+ .. tip ::
332+
333+ If you need to set cache headers for many different controller actions,
334+ you might want to look into the FOSHttpCacheBundle _. It provides a way
335+ to define cache headers based on the URL pattern and other request
336+ properties.
337+
331338Public vs private Responses
332339~~~~~~~~~~~~~~~~~~~~~~~~~~~
333340
@@ -1051,21 +1058,35 @@ Cache Invalidation
10511058 "There are only two hard things in Computer Science: cache invalidation
10521059 and naming things." -- Phil Karlton
10531060
1054- You should never need to invalidate cached data because invalidation is already
1055- taken into account natively in the HTTP cache models. If you use validation,
1056- you never need to invalidate anything by definition; and if you use expiration
1057- and need to invalidate a resource, it means that you set the expires date
1058- too far away in the future.
1061+ Once an URL is cached by a caching reverse proxy, the proxy will not ask the
1062+ application for that content anymore. This allows the cache to do fast
1063+ responses and reduces the load on your application. However, you risk
1064+ delivering outdated content. A way out of this dilemma is to use long
1065+ cache lifetimes, but to actively notify the caching proxy when content
1066+ changes. Reverse proxies usually provide a channel to receive such
1067+ notifications, usually through special HTTP requests.
10591068
1060- .. note ::
1069+ .. tip ::
1070+
1071+ While cache invalidation sounds powerful, avoid it when possible. If you
1072+ fail to invalidate something, outdated caches will stay for a potentially
1073+ long time. Instead, use short cache lifetimes or use the validation model,
1074+ and adjust your controllers to perform efficient validation checks as
1075+ explained in :ref: `optimizing-cache-validation `.
10611076
1062- Since invalidation is a topic specific to each type of reverse proxy,
1063- if you don't worry about invalidation, you can switch between reverse
1064- proxies without changing anything in your application code .
1077+ Furthermore, since invalidation is a topic specific to each type of reverse
1078+ proxy, using this concept will tie you to a specific reverse proxy or need
1079+ additional efforts to support different proxies .
10651080
1066- Actually, all reverse proxies provide ways to purge cached data, but you
1067- should avoid them as much as possible. The most standard way is to purge the
1068- cache for a given URL by requesting it with the special ``PURGE `` HTTP method.
1081+ Sometimes, however, you need that extra performance you can get when
1082+ explicitly invalidating. For invalidation, your application needs to detect
1083+ when content changes and tell the cache to remove the URLs which contain
1084+ that data from its cache.
1085+
1086+ If one content corresponds to one URL, the ``PURGE `` model works well.
1087+ You send a request to the cache proxy with the HTTP method ``PURGE `` instead
1088+ of ``GET `` and make the cache proxy detect this and remove the data from the
1089+ cache instead of going to Symfony to get a response.
10691090
10701091Here is how you can configure the Symfony reverse proxy to support the
10711092``PURGE `` HTTP method::
@@ -1085,11 +1106,15 @@ Here is how you can configure the Symfony reverse proxy to support the
10851106 return parent::invalidate($request, $catch);
10861107 }
10871108
1109+ if ('127.0.0.1' !== $request->getClientIp()) {
1110+ return new Response('Invalid HTTP method', Response::HTTP_BAD_REQUEST);
1111+ }
1112+
10881113 $response = new Response();
10891114 if ($this->getStore()->purge($request->getUri())) {
10901115 $response->setStatusCode(200, 'Purged');
10911116 } else {
1092- $response->setStatusCode(404 , 'Not purged ');
1117+ $response->setStatusCode(200 , 'Not found ');
10931118 }
10941119
10951120 return $response;
@@ -1101,6 +1126,18 @@ Here is how you can configure the Symfony reverse proxy to support the
11011126 You must protect the ``PURGE `` HTTP method somehow to avoid random people
11021127 purging your cached data.
11031128
1129+ In many applications, content is used in various URLs. More flexible concepts
1130+ exist for those cases:
1131+
1132+ * **Banning ** invalidates responses matching regular expressions on the
1133+ URL or other criteria.
1134+ * **Cache tagging ** lets you add a tag for each content used in a response
1135+ so that you can invalidate all URLs containing a certain content.
1136+
1137+ If you need such features, you should use the `FOSHttpCacheBundle `_. This
1138+ bundle documents the configuration for the caching proxy and provides
1139+ services to send invalidation requests based on URLs and Symfony routes.
1140+
11041141Summary
11051142-------
11061143
@@ -1128,3 +1165,4 @@ Learn more from the Cookbook
11281165.. _`P6 - Caching: Browser and intermediary caches` : http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache
11291166.. _`FrameworkExtraBundle documentation` : http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html
11301167.. _`ESI` : http://www.w3.org/TR/esi-lang
1168+ .. _`FOSHttpCacheBundle` : http://foshttpcachebundle.readthedocs.org/
0 commit comments