Skip to content

Commit

Permalink
[BUGFIX] Invalidate backend preview caches on each DataHandler operation
Browse files Browse the repository at this point in the history
Fixes: #171
  • Loading branch information
nhovratov committed Mar 30, 2024
1 parent 3ec6cdb commit 968e839
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Classes/Backend/Preview/PageLayout.php
Expand Up @@ -156,7 +156,7 @@ protected function getContentBlockData(
if ($resolvedData->resolved !== $pageRow) {
try {
$exported = 'return ' . VarExporter::export($resolvedData) . ';';
$this->cache->set($cacheIdentifier, $exported);
$this->cache->set($cacheIdentifier, $exported, ['content_blocks_preview'], 0);
} catch (NotInstantiableTypeException) {
// @todo objects of class TYPO3\CMS\Core\Resource\File can't be exported
// @todo due to attached storage, which itself has EventDispatcher attached
Expand Down
2 changes: 1 addition & 1 deletion Classes/Backend/Preview/PreviewRenderer.php
Expand Up @@ -83,7 +83,7 @@ public function renderPageModulePreviewContent(GridColumnItem $item): string
if ($resolvedData->resolved !== $record) {
try {
$exported = 'return ' . VarExporter::export($resolvedData) . ';';
$this->cache->set($cacheIdentifier, $exported);
$this->cache->set($cacheIdentifier, $exported, ['content_blocks_preview'], 0);
} catch (NotInstantiableTypeException) {
// @todo objects of class TYPO3\CMS\Core\Resource\File can't be exported
// @todo due to attached storage, which itself has EventDispatcher attached
Expand Down
39 changes: 39 additions & 0 deletions Classes/DataHandler/ClearBackendPreviewCaches.php
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\ContentBlocks\DataHandler;

use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\DataHandling\DataHandler;

/**
* @internal Not part of TYPO3's public API.
*/
class ClearBackendPreviewCaches
{
public function __construct(
protected readonly CacheManager $cacheManager,
) {}

public function processDatamap_afterAllOperations(DataHandler $dataHandler): void
{
// This is just a simple solution for invalidating preview caches after
// each DataHandler operation. It is not easy to make selective picks
// as relations can get as complex as needed.
$this->cacheManager->flushCachesByTag('content_blocks_preview');
}
}
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Expand Up @@ -14,6 +14,9 @@ services:
TYPO3\CMS\ContentBlocks\Cache\InitializeContentBlockCache:
public: true

TYPO3\CMS\ContentBlocks\DataHandler\ClearBackendPreviewCaches:
public: true

# @todo change to BeforeTcaOverridesEvent for v13
TYPO3\CMS\ContentBlocks\Generator\TcaGenerator:
public: true
Expand Down
6 changes: 5 additions & 1 deletion ext_localconf.php
Expand Up @@ -2,6 +2,8 @@

defined('TYPO3') or die();

use TYPO3\CMS\ContentBlocks\DataHandler\ClearBackendPreviewCaches;
use TYPO3\CMS\Core\Cache\Backend\FileBackend;
use TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend;
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
Expand Down Expand Up @@ -44,11 +46,13 @@

$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['content_blocks_preview'] = [
'frontend' => PhpFrontend::class,
'backend' => SimpleFileBackend::class,
'backend' => FileBackend::class,
'options' => [
'defaultLifetime' => 0,
],
'groups' => ['system'],
];

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = ClearBackendPreviewCaches::class;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['cb'][] = 'TYPO3\\CMS\\ContentBlocks\\ViewHelpers';

0 comments on commit 968e839

Please sign in to comment.