Repository: pimcore/pimcore (language: php)

Issue:
[Improvement]: Invalidate the full page cache by saving a document
# Improvement description

In our project we had the problem that an updated document wasn't show in the frontend. We use the full page cage and it turned out that this cache isn't invalidate if you update a document. We could change this by adding an other caching tag to the cache entry.


Discussion / hints:

Fix patch:
diff --git a/bundles/CoreBundle/src/EventListener/Frontend/FullPageCacheListener.php b/bundles/CoreBundle/src/EventListener/Frontend/FullPageCacheListener.php
index 4e3477ff83a..8e19180e8d0 100644
--- a/bundles/CoreBundle/src/EventListener/Frontend/FullPageCacheListener.php
+++ b/bundles/CoreBundle/src/EventListener/Frontend/FullPageCacheListener.php
@@ -28,6 +28,7 @@
 use Pimcore\Config;
 use Pimcore\Event\Cache\FullPage\CacheResponseEvent;
 use Pimcore\Event\Cache\FullPage\PrepareResponseEvent;
+use Pimcore\Event\Cache\FullPage\PrepareTagsEvent;
 use Pimcore\Event\FullPageCacheEvents;
 use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
 use Pimcore\Logger;
@@ -342,13 +343,13 @@ public function onKernelResponse(ResponseEvent $event): void
 
                 $event = new PrepareResponseEvent($request, $response);
                 $this->eventDispatcher->dispatch($event, FullPageCacheEvents::PREPARE_RESPONSE);
-
                 $cacheItem = $event->getResponse();
 
-                $tags = ['output'];
-                if ($this->lifetime) {
-                    $tags = ['output_lifetime'];
-                }
+                $event = new PrepareTagsEvent($request, $response);
+                $this->eventDispatcher->dispatch($event, FullPageCacheEvents::PREPARE_TAGS);
+                $tags = $event->getTags();
+
+                $tags[] = $this->lifetime ? 'output_lifetime' : 'output';
 
                 Cache::save($cacheItem, $cacheKey, $tags, $this->lifetime, 1000, true);
             } catch (Exception $e) {
diff --git a/lib/Event/Cache/FullPage/PrepareTagsEvent.php b/lib/Event/Cache/FullPage/PrepareTagsEvent.php
new file mode 100644
index 00000000000..cf7a0412c85
--- /dev/null
+++ b/lib/Event/Cache/FullPage/PrepareTagsEvent.php
@@ -0,0 +1,69 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * Pimcore
+ *
+ * This source file is available under two different licenses:
+ * - GNU General Public License version 3 (GPLv3)
+ * - Pimcore Commercial License (PCL)
+ * Full copyright and license information is available in
+ * LICENSE.md which is distributed with this source code.
+ *
+ *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
+ *  @license    http://www.pimcore.org/license     GPLv3 and PCL
+ */
+
+namespace Pimcore\Event\Cache\FullPage;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Contracts\EventDispatcher\Event;
+
+class PrepareTagsEvent extends Event
+{
+    /**
+     * Tags associated with the cache entry
+     *
+     * @var string[]
+     */
+    private array $tags = [];
+
+    public function __construct(
+        private readonly Request $request,
+        private readonly Response $response
+    ) {
+    }
+
+    public function addTag(string $tag): void
+    {
+        $this->tags[] = $tag;
+    }
+
+    /**
+     * @return string[]
+     */
+    public function getTags(): array
+    {
+        return $this->tags;
+    }
+
+    /**
+     * @param string[] $tags
+     */
+    public function setTags(array $tags): void
+    {
+        $this->tags = $tags;
+    }
+
+    public function getRequest(): Request
+    {
+        return $this->request;
+    }
+
+    public function getResponse(): Response
+    {
+        return $this->response;
+    }
+}
diff --git a/lib/Event/FullPageCacheEvents.php b/lib/Event/FullPageCacheEvents.php
index 2e6a1a3c4fc..e3f9cbd7b1a 100644
--- a/lib/Event/FullPageCacheEvents.php
+++ b/lib/Event/FullPageCacheEvents.php
@@ -48,4 +48,14 @@ final class FullPageCacheEvents
      * @var string
      */
     const PREPARE_RESPONSE = 'pimcore.cache.full_page.prepare_response';
+
+    /**
+     * Fired before the response is written to cache. Can be used to add tags
+     * to the cached response.
+     *
+     * @Event("Pimcore\Event\Cache\FullPage\PrepareTagsEvent")
+     *
+     * @var string
+     */
+    const PREPARE_TAGS = 'pimcore.cache.full_page.prepare_tags';
 }


Test patch: