‪TYPO3CMS  11.5
CacheService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
29 
35 {
41  protected array ‪$hasPidColumn = [];
42  protected array ‪$clearCacheForTables = [];
46  protected \SplStack ‪$pageIdStack;
47 
49  {
50  $this->configurationManager = ‪$configurationManager;
51  $this->cacheManager = ‪$cacheManager;
52  $this->pageIdStack = new \SplStack();
53  $this->connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
54  }
55 
56  public function ‪getPageIdStack(): \SplStack
57  {
58  return ‪$this->pageIdStack;
59  }
60 
66  public function ‪clearPageCache($pageIdsToClear = null): void
67  {
68  if ($pageIdsToClear === null) {
69  $this->cacheManager->flushCachesInGroup('pages');
70  } else {
71  if (!is_array($pageIdsToClear)) {
72  $pageIdsToClear = [(int)$pageIdsToClear];
73  }
74  $tags = array_map(static function ($item) {
75  return 'pageId_' . $item;
76  }, $pageIdsToClear);
77  $this->cacheManager->flushCachesInGroupByTags('pages', $tags);
78  }
79  }
80 
88  public function ‪clearCachesOfRegisteredPageIds(): void
89  {
90  $frameworkConfiguration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
91  if (!empty($frameworkConfiguration['persistence']['enableAutomaticCacheClearing'] ?? false)) {
92  foreach ($this->clearCacheForTables as $table => $ids) {
93  foreach ($ids as $id) {
94  $this->‪clearPageCacheForGivenRecord($table, $id);
95  }
96  }
97  }
98  if (!$this->pageIdStack->isEmpty()) {
99  $pageIds = [];
100  while (!$this->pageIdStack->isEmpty()) {
101  $pageIds[] = (int)$this->pageIdStack->pop();
102  }
103  $pageIds = array_values(array_unique($pageIds));
104  $this->‪clearPageCache($pageIds);
105  }
106  }
107 
117  public function ‪clearCacheForRecord(string $table, int $uid): void
118  {
119  if (!is_array($this->clearCacheForTables[$table] ?? null)) {
120  $this->clearCacheForTables[$table] = [];
121  }
122  $this->clearCacheForTables[$table][] = $uid;
123  }
124 
135  protected function ‪clearPageCacheForGivenRecord(string $tableName, int $uid): void
136  {
137  $pageIdsToClear = [];
138  $storagePage = null;
139 
140  // As determining the table columns is a costly operation this is done only once per table during runtime and cached then
141  if (!isset($this->hasPidColumn[$tableName])) {
142  $columns = $this->connectionPool
143  ->getConnectionForTable($tableName)
144  ->createSchemaManager()
145  ->listTableColumns($tableName);
146  $this->hasPidColumn[$tableName] = array_key_exists('pid', $columns);
147  }
148 
149  if ($this->hasPidColumn[$tableName]) {
150  $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName);
151  $queryBuilder->getRestrictions()->removeAll();
152  $result = $queryBuilder
153  ->select('pid')
154  ->from($tableName)
155  ->where(
156  $queryBuilder->expr()->eq(
157  'uid',
158  $queryBuilder->createNamedParameter($uid, ‪Connection::PARAM_INT)
159  )
160  )
161  ->executeQuery();
162  if ($row = $result->fetchAssociative()) {
163  $storagePage = $row['pid'];
164  $pageIdsToClear[] = $storagePage;
165  }
166  } elseif ($this->‪getTypoScriptFrontendController() !== null) {
167  // No PID column - we can do a best-effort to clear the cache of the current page if in FE
168  $storagePage = $this->‪getTypoScriptFrontendController()->id;
169  $pageIdsToClear[] = $storagePage;
170  }
171  if ($storagePage === null) {
172  return;
173  }
174 
175  $pageTS = BackendUtility::getPagesTSconfig($storagePage);
176  if (isset($pageTS['TCEMAIN.']['clearCacheCmd'])) {
177  $clearCacheCommands = ‪GeneralUtility::trimExplode(',', strtolower($pageTS['TCEMAIN.']['clearCacheCmd']), true);
178  $clearCacheCommands = array_unique($clearCacheCommands);
179  foreach ($clearCacheCommands as $clearCacheCommand) {
180  if (‪MathUtility::canBeInterpretedAsInteger($clearCacheCommand)) {
181  $pageIdsToClear[] = $clearCacheCommand;
182  }
183  }
184  }
185 
186  foreach ($pageIdsToClear as $pageIdToClear) {
187  $this->‪getPageIdStack()->push($pageIdToClear);
188  }
189  }
190 
192  {
193  return ‪$GLOBALS['TSFE'] ?? null;
194  }
195 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Extbase\Service\CacheService\getTypoScriptFrontendController
‪getTypoScriptFrontendController()
Definition: CacheService.php:191
‪TYPO3\CMS\Extbase\Service\CacheService\$configurationManager
‪ConfigurationManagerInterface $configurationManager
Definition: CacheService.php:43
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Extbase\Service\CacheService\__construct
‪__construct(ConfigurationManagerInterface $configurationManager, CacheManager $cacheManager)
Definition: CacheService.php:48
‪TYPO3\CMS\Extbase\Service\CacheService\clearCacheForRecord
‪clearCacheForRecord(string $table, int $uid)
Definition: CacheService.php:117
‪TYPO3\CMS\Extbase\Service\CacheService\$hasPidColumn
‪array $hasPidColumn
Definition: CacheService.php:41
‪TYPO3\CMS\Extbase\Service\CacheService\$cacheManager
‪CacheManager $cacheManager
Definition: CacheService.php:44
‪TYPO3\CMS\Extbase\Service\CacheService\clearPageCacheForGivenRecord
‪clearPageCacheForGivenRecord(string $tableName, int $uid)
Definition: CacheService.php:135
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Service
Definition: CacheService.php:18
‪TYPO3\CMS\Extbase\Service\CacheService\$clearCacheForTables
‪array $clearCacheForTables
Definition: CacheService.php:42
‪TYPO3\CMS\Extbase\Service\CacheService\$connectionPool
‪ConnectionPool $connectionPool
Definition: CacheService.php:45
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:29
‪TYPO3\CMS\Extbase\Service\CacheService\$pageIdStack
‪SplStack $pageIdStack
Definition: CacheService.php:46
‪TYPO3\CMS\Extbase\Service\CacheService\clearPageCache
‪clearPageCache($pageIdsToClear=null)
Definition: CacheService.php:66
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Extbase\Service\CacheService\clearCachesOfRegisteredPageIds
‪clearCachesOfRegisteredPageIds()
Definition: CacheService.php:88
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Service\CacheService
Definition: CacheService.php:35
‪TYPO3\CMS\Extbase\Service\CacheService\getPageIdStack
‪getPageIdStack()
Definition: CacheService.php:56
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50