‪TYPO3CMS  ‪main
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;
28 
34 {
35  protected array ‪$clearCacheForTables = [];
36  protected \SplStack ‪$pageIdStack;
37 
38  public function ‪__construct(
39  private readonly ‪ConfigurationManagerInterface $configurationManager,
40  private readonly ‪CacheManager $cacheManager,
41  private readonly ‪ConnectionPool $connectionPool,
42  ) {
43  $this->pageIdStack = new \SplStack();
44  }
45 
46  public function ‪getPageIdStack(): \SplStack
47  {
48  return ‪$this->pageIdStack;
49  }
50 
56  public function ‪clearPageCache($pageIdsToClear = null): void
57  {
58  if ($pageIdsToClear === null) {
59  $this->cacheManager->flushCachesInGroup('pages');
60  } else {
61  if (!is_array($pageIdsToClear)) {
62  $pageIdsToClear = [(int)$pageIdsToClear];
63  }
64  $tags = array_map(static fn(int $item): string => 'pageId_' . $item, $pageIdsToClear);
65  $this->cacheManager->flushCachesInGroupByTags('pages', $tags);
66  }
67  }
68 
76  public function ‪clearCachesOfRegisteredPageIds(): void
77  {
78  $frameworkConfiguration = $this->configurationManager->getConfiguration(‪ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
79  if (!empty($frameworkConfiguration['persistence']['enableAutomaticCacheClearing'] ?? false)) {
80  foreach ($this->clearCacheForTables as $table => $ids) {
81  foreach ($ids as $id) {
82  $this->‪clearPageCacheForGivenRecord($table, $id);
83  }
84  }
85  }
86  if (!$this->pageIdStack->isEmpty()) {
87  $pageIds = [];
88  while (!$this->pageIdStack->isEmpty()) {
89  $pageIds[] = (int)$this->pageIdStack->pop();
90  }
91  $pageIds = array_values(array_unique($pageIds));
92  $this->‪clearPageCache($pageIds);
93  }
94  }
95 
105  public function ‪clearCacheForRecord(string $table, int ‪$uid): void
106  {
107  if (!is_array($this->clearCacheForTables[$table] ?? null)) {
108  $this->clearCacheForTables[$table] = [];
109  }
110  $this->clearCacheForTables[$table][] = ‪$uid;
111  }
112 
123  protected function ‪clearPageCacheForGivenRecord(string $tableName, int ‪$uid): void
124  {
125  $pageIdsToClear = [];
126  $storagePage = null;
127 
128  $queryBuilder = $this->connectionPool->getQueryBuilderForTable($tableName);
129  $queryBuilder->getRestrictions()->removeAll();
130  $result = $queryBuilder
131  ->select('pid')
132  ->from($tableName)
133  ->where(
134  $queryBuilder->expr()->eq(
135  'uid',
136  $queryBuilder->createNamedParameter(‪$uid, ‪Connection::PARAM_INT)
137  )
138  )
139  ->executeQuery();
140  if ($row = $result->fetchAssociative()) {
141  $storagePage = $row['pid'];
142  $pageIdsToClear[] = $storagePage;
143  }
144  if ($storagePage === null) {
145  return;
146  }
147 
148  $pageTS = BackendUtility::getPagesTSconfig($storagePage);
149  if (isset($pageTS['TCEMAIN.']['clearCacheCmd'])) {
150  $clearCacheCommands = ‪GeneralUtility::trimExplode(',', strtolower((string)$pageTS['TCEMAIN.']['clearCacheCmd']), true);
151  $clearCacheCommands = array_unique($clearCacheCommands);
152  foreach ($clearCacheCommands as $clearCacheCommand) {
153  if (‪MathUtility::canBeInterpretedAsInteger($clearCacheCommand)) {
154  $pageIdsToClear[] = $clearCacheCommand;
155  }
156  }
157  }
158 
159  foreach ($pageIdsToClear as $pageIdToClear) {
160  $this->‪getPageIdStack()->push($pageIdToClear);
161  }
162  }
163 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Extbase\Service\CacheService\clearCacheForRecord
‪clearCacheForRecord(string $table, int $uid)
Definition: CacheService.php:105
‪TYPO3\CMS\Extbase\Service\CacheService\clearPageCacheForGivenRecord
‪clearPageCacheForGivenRecord(string $tableName, int $uid)
Definition: CacheService.php:123
‪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:35
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:29
‪TYPO3\CMS\Extbase\Service\CacheService\__construct
‪__construct(private readonly ConfigurationManagerInterface $configurationManager, private readonly CacheManager $cacheManager, private readonly ConnectionPool $connectionPool,)
Definition: CacheService.php:38
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Extbase\Service\CacheService\$pageIdStack
‪SplStack $pageIdStack
Definition: CacheService.php:36
‪TYPO3\CMS\Extbase\Service\CacheService\clearPageCache
‪clearPageCache($pageIdsToClear=null)
Definition: CacheService.php:56
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Extbase\Service\CacheService\clearCachesOfRegisteredPageIds
‪clearCachesOfRegisteredPageIds()
Definition: CacheService.php:76
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Extbase\Service\CacheService
Definition: CacheService.php:34
‪TYPO3\CMS\Extbase\Service\CacheService\getPageIdStack
‪getPageIdStack()
Definition: CacheService.php:46
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822