From 34809dde62fbf08baa68646f9664d10713fc7a8e Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 26 Feb 2023 17:31:21 +0100 Subject: [PATCH] [HttpCache] Explain how to extend the HttpCache class --- http_cache/cache_invalidation.rst | 57 +++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/http_cache/cache_invalidation.rst b/http_cache/cache_invalidation.rst index 6a11a1fdc78..c6df4fd85c0 100644 --- a/http_cache/cache_invalidation.rst +++ b/http_cache/cache_invalidation.rst @@ -47,8 +47,9 @@ the word "PURGE" is a convention, technically this can be any string) instead of ``GET`` and make the cache proxy detect this and remove the data from the cache instead of going to the application to get a response. -Here is how you can configure the Symfony reverse proxy (See :doc:`/http_cache`) -to support the ``PURGE`` HTTP method:: +Here is how you can configure the :ref:`Symfony reverse proxy ` +to support the ``PURGE`` HTTP method. First create a caching kernel that overrides the +:method:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache::invalidate` method:: // src/CacheKernel.php namespace App; @@ -84,6 +85,58 @@ to support the ``PURGE`` HTTP method:: } } +Then, register the class as a service that :doc:`decorates ` +``http_cache``:: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + App\CacheKernel: + decorates: http_cache + arguments: + - '@kernel' + - '@http_cache.store' + - '@?esi' + + .. code-block:: xml + + + + + + + + + + + + .. code-block:: php + + // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + use App\CacheKernel; + + return function (ContainerConfigurator $containerConfigurator) { + $services = $containerConfigurator->services(); + + $services->set(CacheKernel::class) + ->decorate('http_cache') + ->args([ + service('kernel'), + service('http_cache.store'), + service('esi')->nullOnInvalid(), + ]) + ; + }; + .. caution:: You must protect the ``PURGE`` HTTP method somehow to avoid random people