‪TYPO3CMS  ‪main
ClearCacheService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
29 {
30  private const ‪legacyDatabaseCacheTables = [
31  'cache_treelist',
32  ];
33 
34  public function ‪__construct(
35  private readonly ‪LateBootService $lateBootService,
36  private readonly ‪FrontendInterface $dependencyInjectionCache
37  ) {}
38 
52  public function ‪clearAll(): void
53  {
54  // Low level flush of legacy database cache tables
55  $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
56  foreach (self::legacyDatabaseCacheTables as $tableName) {
57  $connection = $connectionPool->getConnectionForTable($tableName);
58  if ($connection->createSchemaManager()->tablesExist($tableName)) {
59  $connection->truncate($tableName);
60  }
61  }
62 
63  // Flush all caches defined in TYPO3_CONF_VARS, but not the ones defined by extensions in ext_localconf.php
64  $baseCaches = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] ?? [];
65  $this->‪flushCaches($baseCaches);
66 
67  // Remove DI container cache (this might be removed in preference of functionality to rebuild this cache)
68  if ($this->dependencyInjectionCache->getBackend() instanceof ‪ContainerBackend) {
70  $diCacheBackend = $this->dependencyInjectionCache->getBackend();
71  // We need to remove using the forceFlush method because the DI cache backend disables the flush method
72  $diCacheBackend->forceFlush();
73  }
74 
75  // From this point on, the code may fatal, if some broken extension is loaded.
76  $this->lateBootService->loadExtLocalconfDatabaseAndExtTables();
77 
78  $extensionCaches = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] ?? [];
79  // Loose comparison on purpose to allow changed ordering of the array
80  if ($baseCaches != $extensionCaches) {
81  // When configuration has changed during loading of extensions (due to ext_localconf.php), flush all caches again
82  $this->‪flushCaches(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
83  }
84  }
85 
94  private function ‪flushCaches(array $cacheConfiguration): void
95  {
96  $cacheManager = new ‪CacheManager();
97  $cacheManager->setCacheConfigurations($cacheConfiguration);
98  $cacheManager->flushCaches();
99  }
100 }
‪TYPO3\CMS\Core\DependencyInjection\Cache\ContainerBackend
Definition: ContainerBackend.php:26
‪TYPO3\CMS\Install\Service\ClearCacheService\flushCaches
‪flushCaches(array $cacheConfiguration)
Definition: ClearCacheService.php:94
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Install\Service\ClearCacheService\legacyDatabaseCacheTables
‪const legacyDatabaseCacheTables
Definition: ClearCacheService.php:30
‪TYPO3\CMS\Install\Service\LateBootService
Definition: LateBootService.php:27
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Install\Service\ClearCacheService\__construct
‪__construct(private readonly LateBootService $lateBootService, private readonly FrontendInterface $dependencyInjectionCache)
Definition: ClearCacheService.php:34
‪TYPO3\CMS\Install\Service\ClearCacheService\clearAll
‪clearAll()
Definition: ClearCacheService.php:52
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Install\Service\ClearCacheService
Definition: ClearCacheService.php:29
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16