‪TYPO3CMS  ‪main
CacheManager.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 
16 namespace ‪TYPO3\CMS\Core\Cache;
17 
31 
36 {
40  protected ‪$caches = [];
41 
45  protected ‪$cacheConfigurations = [];
46 
55  protected ‪$cacheGroups = [];
56 
60  protected ‪$defaultCacheConfiguration = [
61  'frontend' => VariableFrontend::class,
62  'backend' => Typo3DatabaseBackend::class,
63  'options' => [],
64  'groups' => ['all'],
65  ];
66 
70  protected ‪$disableCaching = false;
71 
72  public function ‪__construct(bool ‪$disableCaching = false)
73  {
74  $this->disableCaching = ‪$disableCaching;
75  }
76 
93  {
94  $newConfiguration = [];
95  foreach (‪$cacheConfigurations as ‪$identifier => $configuration) {
96  if (empty(‪$identifier)) {
97  throw new \InvalidArgumentException('A cache identifier was not set.', 1596980032);
98  }
99  if (!is_array($configuration)) {
100  throw new \InvalidArgumentException('The cache configuration for cache "' . ‪$identifier . '" was not an array as expected.', 1231259656);
101  }
102  $newConfiguration[‪$identifier] = $configuration;
103  }
104  $this->cacheConfigurations = $newConfiguration;
105  }
106 
114  public function ‪registerCache(FrontendInterface $cache, array $groups = [])
115  {
116  ‪$identifier = $cache->getIdentifier();
117  if (isset($this->caches[‪$identifier])) {
118  throw new DuplicateIdentifierException('A cache with identifier "' . ‪$identifier . '" has already been registered.', 1203698223);
119  }
120  $this->caches[‪$identifier] = $cache;
121  foreach ($groups as $groupIdentifier) {
122  $this->cacheGroups[$groupIdentifier][] = ‪$identifier;
123  }
124  }
125 
133  public function ‪getCache(‪$identifier)
134  {
135  if ($this->‪hasCache($identifier) === false) {
136  throw new NoSuchCacheException('A cache with identifier "' . ‪$identifier . '" does not exist.', 1203699034);
137  }
138  if (!isset($this->caches[‪$identifier])) {
139  $this->‪createCache($identifier);
140  }
141  return $this->caches[‪$identifier];
142  }
143 
150  public function ‪hasCache(‪$identifier)
151  {
152  return isset($this->caches[‪$identifier]) || isset($this->cacheConfigurations[‪$identifier]);
153  }
154 
158  public function ‪flushCaches()
159  {
160  $this->‪createAllCaches();
161  foreach ($this->caches as $cache) {
162  $cache->flush();
163  }
164  }
165 
172  public function ‪flushCachesInGroup($groupIdentifier)
173  {
174  $this->‪createAllCaches();
175  if (!isset($this->cacheGroups[$groupIdentifier])) {
176  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390334120);
177  }
178  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
179  if (isset($this->caches[$cacheIdentifier])) {
180  $this->caches[$cacheIdentifier]->flush();
181  }
182  }
183  }
184 
193  public function ‪flushCachesInGroupByTag($groupIdentifier, $tag)
194  {
195  if (empty($tag)) {
196  return;
197  }
198  $this->‪createAllCaches();
199  if (!isset($this->cacheGroups[$groupIdentifier])) {
200  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390337129);
201  }
202  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
203  if (isset($this->caches[$cacheIdentifier])) {
204  $this->caches[$cacheIdentifier]->flushByTag($tag);
205  }
206  }
207  }
208 
217  public function ‪flushCachesInGroupByTags($groupIdentifier, array $tags)
218  {
219  if (empty($tags)) {
220  return;
221  }
222  $this->‪createAllCaches();
223  if (!isset($this->cacheGroups[$groupIdentifier])) {
224  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390337130);
225  }
226  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
227  if (isset($this->caches[$cacheIdentifier])) {
228  $this->caches[$cacheIdentifier]->flushByTags($tags);
229  }
230  }
231  }
232 
239  public function ‪flushCachesByTag($tag)
240  {
241  $this->‪createAllCaches();
242  foreach ($this->caches as $cache) {
243  $cache->flushByTag($tag);
244  }
245  }
246 
252  public function ‪flushCachesByTags(array $tags)
253  {
254  $this->‪createAllCaches();
255  foreach ($this->caches as $cache) {
256  $cache->flushByTags($tags);
257  }
258  }
259 
264  public function ‪getCacheGroups(): array
265  {
266  $groups = array_keys($this->cacheGroups);
267 
268  foreach ($this->cacheConfigurations as $config) {
269  foreach ($config['groups'] ?? [] as $group) {
270  if (!in_array($group, $groups, true)) {
271  $groups[] = $group;
272  }
273  }
274  }
275 
276  return $groups;
277  }
278 
279  public function ‪handleCacheFlushEvent(CacheFlushEvent $event): void
280  {
281  foreach ($event->getGroups() as $group) {
282  $this->‪flushCachesInGroup($group);
283  }
284  }
285 
289  protected function ‪createAllCaches()
290  {
291  foreach ($this->cacheConfigurations as ‪$identifier => $_) {
292  if (!isset($this->caches[‪$identifier])) {
293  $this->‪createCache($identifier);
294  }
295  }
296  }
297 
306  protected function ‪createCache(‪$identifier)
307  {
308  if (isset($this->cacheConfigurations[‪$identifier]['frontend'])) {
309  $frontend = $this->cacheConfigurations[‪$identifier]['frontend'];
310  } else {
311  $frontend = $this->defaultCacheConfiguration['frontend'];
312  }
313  if (isset($this->cacheConfigurations[‪$identifier]['backend'])) {
314  $backend = $this->cacheConfigurations[‪$identifier]['backend'];
315  } else {
316  $backend = $this->defaultCacheConfiguration['backend'];
317  }
318  if (isset($this->cacheConfigurations[‪$identifier]['options'])) {
319  $backendOptions = $this->cacheConfigurations[‪$identifier]['options'];
320  } else {
321  $backendOptions = $this->defaultCacheConfiguration['options'];
322  }
323 
324  if ($this->disableCaching && $backend !== TransientMemoryBackend::class) {
325  $backend = NullBackend::class;
326  $backendOptions = [];
327  }
328 
329  // Add the cache identifier to the groups that it should be attached to, or use the default ones.
330  if (isset($this->cacheConfigurations[‪$identifier]['groups']) && is_array($this->cacheConfigurations[‪$identifier]['groups'])) {
331  $assignedGroups = $this->cacheConfigurations[‪$identifier]['groups'];
332  } else {
333  $assignedGroups = $this->defaultCacheConfiguration['groups'];
334  }
335  foreach ($assignedGroups as $groupIdentifier) {
336  if (!isset($this->cacheGroups[$groupIdentifier])) {
337  $this->cacheGroups[$groupIdentifier] = [];
338  }
339  $this->cacheGroups[$groupIdentifier][] = ‪$identifier;
340  }
341 
342  // New operator used on purpose: This class is required early during
343  // bootstrap before makeInstance() is properly set up
344  $backend = '\\' . ltrim($backend, '\\');
345  $backendInstance = new $backend('production', $backendOptions);
346  if (!$backendInstance instanceof BackendInterface) {
347  throw new InvalidBackendException('"' . $backend . '" is not a valid cache backend object.', 1464550977);
348  }
349  if (is_callable([$backendInstance, 'initializeObject'])) {
350  $backendInstance->initializeObject();
351  }
352 
353  // New used on purpose, see comment above
354  $frontendInstance = new $frontend(‪$identifier, $backendInstance);
355  if (!$frontendInstance instanceof FrontendInterface) {
356  throw new InvalidCacheException('"' . $frontend . '" is not a valid cache frontend object.', 1464550984);
357  }
358  if (is_callable([$frontendInstance, 'initializeObject'])) {
359  $frontendInstance->initializeObject();
360  }
361 
362  $this->‪registerCache($frontendInstance);
363  }
364 }
‪TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend
Definition: TransientMemoryBackend.php:25
‪TYPO3\CMS\Core\Cache
‪TYPO3\CMS\Core\Cache\CacheManager\$cacheGroups
‪array $cacheGroups
Definition: CacheManager.php:52
‪TYPO3\CMS\Core\Cache\CacheManager\getCache
‪FrontendInterface getCache($identifier)
Definition: CacheManager.php:128
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesInGroupByTags
‪flushCachesInGroupByTags($groupIdentifier, array $tags)
Definition: CacheManager.php:212
‪TYPO3\CMS\Core\Cache\CacheManager\createCache
‪createCache($identifier)
Definition: CacheManager.php:301
‪TYPO3\CMS\Core\Cache\CacheManager\createAllCaches
‪createAllCaches()
Definition: CacheManager.php:284
‪TYPO3\CMS\Core\Cache\Exception\InvalidBackendException
Definition: InvalidBackendException.php:23
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesByTags
‪flushCachesByTags(array $tags)
Definition: CacheManager.php:247
‪TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend
Definition: Typo3DatabaseBackend.php:30
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
Definition: NoSuchCacheException.php:23
‪TYPO3\CMS\Core\Cache\CacheManager\$cacheConfigurations
‪array $cacheConfigurations
Definition: CacheManager.php:43
‪TYPO3\CMS\Core\Cache\CacheManager\hasCache
‪bool hasCache($identifier)
Definition: CacheManager.php:145
‪TYPO3\CMS\Core\Cache\CacheManager\handleCacheFlushEvent
‪handleCacheFlushEvent(CacheFlushEvent $event)
Definition: CacheManager.php:274
‪TYPO3\CMS\Core\Cache\Event\CacheFlushEvent\getGroups
‪getGroups()
Definition: CacheFlushEvent.php:29
‪TYPO3\CMS\Core\Cache\Backend\BackendInterface
Definition: BackendInterface.php:25
‪TYPO3\CMS\Core\Cache\CacheManager\$disableCaching
‪bool $disableCaching
Definition: CacheManager.php:65
‪TYPO3\CMS\Core\Cache\CacheManager\flushCaches
‪flushCaches()
Definition: CacheManager.php:153
‪TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
Definition: VariableFrontend.php:25
‪TYPO3\CMS\Core\Cache\Backend\NullBackend
Definition: NullBackend.php:22
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesInGroupByTag
‪flushCachesInGroupByTag($groupIdentifier, $tag)
Definition: CacheManager.php:188
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesInGroup
‪flushCachesInGroup($groupIdentifier)
Definition: CacheManager.php:167
‪TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException
Definition: DuplicateIdentifierException.php:23
‪TYPO3\CMS\Core\Cache\Exception\InvalidCacheException
Definition: InvalidCacheException.php:23
‪TYPO3\CMS\Core\Cache\CacheManager\$caches
‪FrontendInterface[] $caches
Definition: CacheManager.php:39
‪TYPO3\CMS\Core\Cache\CacheManager\$defaultCacheConfiguration
‪array $defaultCacheConfiguration
Definition: CacheManager.php:56
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Cache\CacheManager\registerCache
‪registerCache(FrontendInterface $cache, array $groups=[])
Definition: CacheManager.php:109
‪TYPO3\CMS\Core\Cache\Event\CacheFlushEvent
Definition: CacheFlushEvent.php:24
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
Definition: NoSuchCacheGroupException.php:23
‪TYPO3\CMS\Core\Cache\CacheManager\setCacheConfigurations
‪setCacheConfigurations(array $cacheConfigurations)
Definition: CacheManager.php:87
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesByTag
‪flushCachesByTag($tag)
Definition: CacheManager.php:234
‪TYPO3\CMS\Core\Cache\CacheManager\__construct
‪__construct(bool $disableCaching=false)
Definition: CacheManager.php:67
‪TYPO3\CMS\Core\Cache\CacheManager\getCacheGroups
‪string[] getCacheGroups()
Definition: CacheManager.php:259
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37