Skip to content

Commit e217f00

Browse files
committed
feature #17972 [HttpCache] Explain how to extend the HttpCache class (HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpCache] Explain how to extend the `HttpCache` class Followup to #16402. In versions of Symfony prior to 5.4 the caching kernel was enabled by wrapping the default kernel into it inside the front controller, eg: ```php // public/index.php use App\CacheKernel; use App\Kernel; // ... $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); // Wrap the default Kernel with the CacheKernel one in 'prod' environment if ('prod' === $kernel->getEnvironment()) { $kernel = new CacheKernel($kernel); } ``` Starting from version 5.4 this was changed to be done through the configuration: ```yaml # config/packages/framework.yaml when@prod: framework: http_cache: true ``` However, it was never explained how the caching kernel can be extended in this case. Decorating the `http_cache` service seems like the best option. Commits ------- 34809dd [HttpCache] Explain how to extend the HttpCache class
2 parents 3e76637 + 34809dd commit e217f00

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)