Skip to content

Commit 34809dd

Browse files
committed
[HttpCache] Explain how to extend the HttpCache class
1 parent c10d923 commit 34809dd

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

http_cache/cache_invalidation.rst

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ the word "PURGE" is a convention, technically this can be any string) instead
4747
of ``GET`` and make the cache proxy detect this and remove the data from the
4848
cache instead of going to the application to get a response.
4949

50-
Here is how you can configure the Symfony reverse proxy (See :doc:`/http_cache`)
51-
to support the ``PURGE`` HTTP method::
50+
Here is how you can configure the :ref:`Symfony reverse proxy <symfony-gateway-cache>`
51+
to support the ``PURGE`` HTTP method. First create a caching kernel that overrides the
52+
:method:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache::invalidate` method::
5253

5354
// src/CacheKernel.php
5455
namespace App;
@@ -84,6 +85,58 @@ to support the ``PURGE`` HTTP method::
8485
}
8586
}
8687

88+
Then, register the class as a service that :doc:`decorates </service_container/service_decoration>`
89+
``http_cache``::
90+
91+
.. configuration-block::
92+
93+
.. code-block:: yaml
94+
95+
# config/services.yaml
96+
services:
97+
App\CacheKernel:
98+
decorates: http_cache
99+
arguments:
100+
- '@kernel'
101+
- '@http_cache.store'
102+
- '@?esi'
103+
104+
.. code-block:: xml
105+
106+
<!-- config/services.xml -->
107+
<?xml version="1.0" encoding="UTF-8" ?>
108+
<container xmlns="http://symfony.com/schema/dic/services"
109+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
110+
xsi:schemaLocation="http://symfony.com/schema/dic/services
111+
https://symfony.com/schema/dic/services/services-1.0.xsd"
112+
>
113+
<service id="App\CacheKernel" decorates="http_cache">
114+
<argument type="service" id="kernel"/>
115+
<argument type="service" id="http_cache.store"/>
116+
<argument type="service" id="esi" on-invalid="null"/>
117+
</service>
118+
</container>
119+
120+
.. code-block:: php
121+
122+
// config/services.php
123+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
124+
125+
use App\CacheKernel;
126+
127+
return function (ContainerConfigurator $containerConfigurator) {
128+
$services = $containerConfigurator->services();
129+
130+
$services->set(CacheKernel::class)
131+
->decorate('http_cache')
132+
->args([
133+
service('kernel'),
134+
service('http_cache.store'),
135+
service('esi')->nullOnInvalid(),
136+
])
137+
;
138+
};
139+
87140
.. caution::
88141

89142
You must protect the ``PURGE`` HTTP method somehow to avoid random people

0 commit comments

Comments
 (0)