@@ -328,6 +328,13 @@ its creation more manageable::
328
328
// set a custom Cache-Control directive
329
329
$response->headers->addCacheControlDirective('must-revalidate', true);
330
330
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
+
331
338
Public vs private Responses
332
339
~~~~~~~~~~~~~~~~~~~~~~~~~~~
333
340
@@ -1051,21 +1058,35 @@ Cache Invalidation
1051
1058
"There are only two hard things in Computer Science: cache invalidation
1052
1059
and naming things." -- Phil Karlton
1053
1060
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.
1059
1068
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 `.
1061
1076
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 .
1065
1080
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.
1069
1090
1070
1091
Here is how you can configure the Symfony reverse proxy to support the
1071
1092
``PURGE `` HTTP method::
@@ -1085,11 +1106,15 @@ Here is how you can configure the Symfony reverse proxy to support the
1085
1106
return parent::invalidate($request, $catch);
1086
1107
}
1087
1108
1109
+ if ('127.0.0.1' !== $request->getClientIp()) {
1110
+ return new Response('Invalid HTTP method', Response::HTTP_BAD_REQUEST);
1111
+ }
1112
+
1088
1113
$response = new Response();
1089
1114
if ($this->getStore()->purge($request->getUri())) {
1090
1115
$response->setStatusCode(200, 'Purged');
1091
1116
} else {
1092
- $response->setStatusCode(404 , 'Not purged ');
1117
+ $response->setStatusCode(200 , 'Not found ');
1093
1118
}
1094
1119
1095
1120
return $response;
@@ -1101,6 +1126,18 @@ Here is how you can configure the Symfony reverse proxy to support the
1101
1126
You must protect the ``PURGE `` HTTP method somehow to avoid random people
1102
1127
purging your cached data.
1103
1128
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
+
1104
1141
Summary
1105
1142
-------
1106
1143
@@ -1128,3 +1165,4 @@ Learn more from the Cookbook
1128
1165
.. _`P6 - Caching: Browser and intermediary caches` : http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache
1129
1166
.. _`FrameworkExtraBundle documentation` : http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html
1130
1167
.. _`ESI` : http://www.w3.org/TR/esi-lang
1168
+ .. _`FOSHttpCacheBundle` : http://foshttpcachebundle.readthedocs.org/
0 commit comments