Search code examples
typo3typo3-9.x

Typo3 9 - clear cache for a page id


For Typo3 8 following approach worked:

$tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
$tce->clear_cacheCmd($pid);

Using this in Typo3 9 causes this error:

Call to a member function getTSConfig() on null
in E:\wwwroot_T9LTS\typo3\sysext\core\Classes\DataHandling\DataHandler.php line 8971

What is the best approach to clear cache for a page id in Typo3 9?

Update
got it to work using CacheManager:

$cacheManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
$cacheManager->flushCachesInGroupByTags('pages', [ 'pageId_'.$pid ]);

Solution

  • You probably run into problems using the DataHandler to clear the cache when not logged in to the backend. In Extbase context you may use:

    $cacheManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Service\CacheService::class);
    $cacheManager->clearPageCache([1,2,3]);