Skip to content

Commit 2bddbb1

Browse files
committed
move invalidation up into expiration and validation section
1 parent d4d2236 commit 2bddbb1

File tree

1 file changed

+100
-93
lines changed

1 file changed

+100
-93
lines changed

book/http_cache.rst

Lines changed: 100 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ its creation more manageable::
335335
to define cache headers based on the URL pattern and other request
336336
properties.
337337

338-
Public vs private Responses
338+
Public vs Private Responses
339339
~~~~~~~~~~~~~~~~~~~~~~~~~~~
340340

341341
Both gateway and proxy caches are considered "shared" caches as the cached
@@ -801,7 +801,105 @@ Additionally, most cache-related HTTP headers can be set via the single
801801
));
802802

803803
.. index::
804-
single: Cache; ESI
804+
single: Cache; Invalidation
805+
806+
.. _http-cache-invalidation:
807+
808+
Cache Invalidation
809+
~~~~~~~~~~~~~~~~~~
810+
811+
"There are only two hard things in Computer Science: cache invalidation
812+
and naming things." -- Phil Karlton
813+
814+
Once an URL is cached by a gateway cache, the cache will not ask the
815+
application for that content anymore. This allows the cache to provide fast
816+
responses and reduces the load on your application. However, you risk
817+
delivering outdated content. A way out of this dilemma is to use long
818+
cache lifetimes, but to actively notify the gateway cache when content
819+
changes. Reverse proxies usually provide a channel to receive such
820+
notifications, typically through special HTTP requests.
821+
822+
.. tip::
823+
824+
While cache invalidation is powerful, avoid it when possible. If you fail
825+
to invalidate something, outdated caches will be served for a potentially
826+
long time. Instead, use short cache lifetimes or use the validation model,
827+
and adjust your controllers to perform efficient validation checks as
828+
explained in :ref:`optimizing-cache-validation`.
829+
830+
Furthermore, since invalidation is a topic specific to each type of reverse
831+
proxy, using this concept will tie you to a specific reverse proxy or need
832+
additional efforts to support different proxies.
833+
834+
Sometimes, however, you need that extra performance you can get when
835+
explicitly invalidating. For invalidation, your application needs to detect
836+
when content changes and tell the cache to remove the URLs which contain
837+
that data from its cache.
838+
839+
If one content corresponds to one URL, the ``PURGE`` model works well.
840+
You send a request to the cache proxy with the HTTP method ``PURGE`` instead
841+
of ``GET`` and make the cache proxy detect this and remove the data from the
842+
cache instead of going to Symfony to get a response.
843+
844+
Here is how you can configure the Symfony reverse proxy to support the
845+
``PURGE`` HTTP method::
846+
847+
// app/AppCache.php
848+
849+
// ...
850+
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
851+
use Symfony\Component\HttpFoundation\Request;
852+
use Symfony\Component\HttpFoundation\Response;
853+
854+
class AppCache extends HttpCache
855+
{
856+
protected function invalidate(Request $request, $catch = false)
857+
{
858+
if ('PURGE' !== $request->getMethod()) {
859+
return parent::invalidate($request, $catch);
860+
}
861+
862+
if ('127.0.0.1' !== $request->getClientIp()) {
863+
return new Response('Invalid HTTP method', Response::HTTP_BAD_REQUEST);
864+
}
865+
866+
$response = new Response();
867+
if ($this->getStore()->purge($request->getUri())) {
868+
$response->setStatusCode(200, 'Purged');
869+
} else {
870+
$response->setStatusCode(200, 'Not found');
871+
}
872+
873+
return $response;
874+
}
875+
}
876+
877+
.. caution::
878+
879+
You must protect the ``PURGE`` HTTP method somehow to avoid random people
880+
purging your cached data.
881+
882+
**Purge** instructs the cache to drop a resource in *all its variants*
883+
(according to the ``Vary`` header, see above). An alternative to purging is
884+
**refreshing** a content. Refreshing means that the caching proxy is
885+
instructed to discard its local cache and fetch the content again. This way,
886+
the new content is already available in the cache. The drawback of refreshing
887+
is that variants are not invalidated.
888+
889+
In many applications, the same content bit is used on various pages with
890+
different URLs. More flexible concepts exist for those cases:
891+
892+
* **Banning** invalidates responses matching regular expressions on the
893+
URL or other criteria.
894+
* **Cache tagging** lets you add a tag for each content used in a response
895+
so that you can invalidate all URLs containing a certain content.
896+
897+
If you need such features, you should use the `FOSHttpCacheBundle`_. This
898+
bundle documents the configuration for the caching proxy and provides
899+
services to send invalidation requests based on URLs and Symfony routes.
900+
901+
.. index::
902+
single: Cache; ESI
805903
single: ESI
806904

807905
.. _edge-side-includes:
@@ -1047,97 +1145,6 @@ The ``render_esi`` helper supports two other useful options:
10471145
to the ESI with a value of ``continue`` indicating that, in the event of
10481146
a failure, the gateway cache will simply remove the ESI tag silently.
10491147

1050-
.. index::
1051-
single: Cache; Invalidation
1052-
1053-
.. _http-cache-invalidation:
1054-
1055-
Cache Invalidation
1056-
------------------
1057-
1058-
"There are only two hard things in Computer Science: cache invalidation
1059-
and naming things." -- Phil Karlton
1060-
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.
1068-
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`.
1076-
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.
1080-
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.
1090-
1091-
Here is how you can configure the Symfony reverse proxy to support the
1092-
``PURGE`` HTTP method::
1093-
1094-
// app/AppCache.php
1095-
1096-
// ...
1097-
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
1098-
use Symfony\Component\HttpFoundation\Request;
1099-
use Symfony\Component\HttpFoundation\Response;
1100-
1101-
class AppCache extends HttpCache
1102-
{
1103-
protected function invalidate(Request $request, $catch = false)
1104-
{
1105-
if ('PURGE' !== $request->getMethod()) {
1106-
return parent::invalidate($request, $catch);
1107-
}
1108-
1109-
if ('127.0.0.1' !== $request->getClientIp()) {
1110-
return new Response('Invalid HTTP method', Response::HTTP_BAD_REQUEST);
1111-
}
1112-
1113-
$response = new Response();
1114-
if ($this->getStore()->purge($request->getUri())) {
1115-
$response->setStatusCode(200, 'Purged');
1116-
} else {
1117-
$response->setStatusCode(200, 'Not found');
1118-
}
1119-
1120-
return $response;
1121-
}
1122-
}
1123-
1124-
.. caution::
1125-
1126-
You must protect the ``PURGE`` HTTP method somehow to avoid random people
1127-
purging your cached data.
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-
11411148
Summary
11421149
-------
11431150

0 commit comments

Comments
 (0)