‪TYPO3CMS  9.5
CacheManager.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
29 
34 {
38  protected ‪$caches = [];
39 
43  protected ‪$cacheConfigurations = [];
44 
53  protected ‪$cacheGroups = [];
54 
58  protected ‪$defaultCacheConfiguration = [
59  'frontend' => VariableFrontend::class,
60  'backend' => Typo3DatabaseBackend::class,
61  'options' => [],
62  'groups' => ['all']
63  ];
64 
68  protected ‪$disableCaching = false;
69 
78  protected ‪$limbo = false;
79 
83  public function ‪__construct(bool ‪$disableCaching = false)
84  {
85  $this->disableCaching = ‪$disableCaching;
86  }
87 
104  {
105  foreach (‪$cacheConfigurations as $identifier => $configuration) {
106  if (!is_array($configuration)) {
107  throw new \InvalidArgumentException('The cache configuration for cache "' . $identifier . '" was not an array as expected.', 1231259656);
108  }
109  $this->cacheConfigurations[$identifier] = $configuration;
110  }
111  }
112 
119  public function ‪registerCache(FrontendInterface $cache)
120  {
121  $identifier = $cache->getIdentifier();
122  if (isset($this->caches[$identifier])) {
123  throw new DuplicateIdentifierException('A cache with identifier "' . $identifier . '" has already been registered.', 1203698223);
124  }
125  $this->caches[$identifier] = $cache;
126  }
127 
135  public function ‪getCache($identifier)
136  {
137  if ($this->‪hasCache($identifier) === false) {
138  throw new NoSuchCacheException('A cache with identifier "' . $identifier . '" does not exist.', 1203699034);
139  }
140  if (!isset($this->caches[$identifier])) {
141  $this->‪createCache($identifier);
142  }
143  return $this->caches[$identifier];
144  }
145 
152  public function ‪hasCache($identifier)
153  {
154  return isset($this->caches[$identifier]) || isset($this->cacheConfigurations[$identifier]);
155  }
156 
160  public function ‪flushCaches()
161  {
162  $this->‪createAllCaches();
163  foreach ($this->caches as $cache) {
164  $cache->flush();
165  }
166  }
167 
174  public function ‪flushCachesInGroup($groupIdentifier)
175  {
176  $this->‪createAllCaches();
177  if (!isset($this->cacheGroups[$groupIdentifier])) {
178  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390334120);
179  }
180  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
181  if (isset($this->caches[$cacheIdentifier])) {
182  $this->caches[$cacheIdentifier]->flush();
183  }
184  }
185  }
186 
195  public function ‪flushCachesInGroupByTag($groupIdentifier, $tag)
196  {
197  if (empty($tag)) {
198  return;
199  }
200  $this->‪createAllCaches();
201  if (!isset($this->cacheGroups[$groupIdentifier])) {
202  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390337129);
203  }
204  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
205  if (isset($this->caches[$cacheIdentifier])) {
206  $this->caches[$cacheIdentifier]->flushByTag($tag);
207  }
208  }
209  }
210 
219  public function ‪flushCachesInGroupByTags($groupIdentifier, array $tags)
220  {
221  if (empty($tags)) {
222  return;
223  }
224  $this->‪createAllCaches();
225  if (!isset($this->cacheGroups[$groupIdentifier])) {
226  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390337130);
227  }
228  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
229  if (isset($this->caches[$cacheIdentifier])) {
230  $this->caches[$cacheIdentifier]->flushByTags($tags);
231  }
232  }
233  }
234 
241  public function ‪flushCachesByTag($tag)
242  {
243  $this->‪createAllCaches();
244  foreach ($this->caches as $cache) {
245  $cache->flushByTag($tag);
246  }
247  }
248 
254  public function ‪flushCachesByTags(array $tags)
255  {
256  $this->‪createAllCaches();
257  foreach ($this->caches as $cache) {
258  $cache->flushByTags($tags);
259  }
260  }
261 
265  protected function ‪createAllCaches()
266  {
267  foreach ($this->cacheConfigurations as $identifier => $_) {
268  if (!isset($this->caches[$identifier])) {
269  $this->‪createCache($identifier);
270  }
271  }
272  }
273 
282  protected function ‪createCache($identifier)
283  {
284  // @deprecated will be removed with TYPO3 v10.0
285  if ($this->limbo) {
286  trigger_error('Usage of ' . self::class . '->createCache(\'' . $identifier . '\') in ext_localconf.php will not be supported in ‪TYPO3 v10.0.', E_USER_DEPRECATED);
287  }
288  if (isset($this->cacheConfigurations[$identifier]['frontend'])) {
289  $frontend = $this->cacheConfigurations[$identifier]['frontend'];
290  } else {
291  $frontend = $this->defaultCacheConfiguration['frontend'];
292  }
293  if (isset($this->cacheConfigurations[$identifier]['backend'])) {
294  $backend = $this->cacheConfigurations[$identifier]['backend'];
295  } else {
296  $backend = $this->defaultCacheConfiguration['backend'];
297  }
298  if (isset($this->cacheConfigurations[$identifier]['options'])) {
299  $backendOptions = $this->cacheConfigurations[$identifier]['options'];
300  } else {
301  $backendOptions = $this->defaultCacheConfiguration['options'];
302  }
303 
304  if ($this->disableCaching && $backend !== TransientMemoryBackend::class) {
305  $backend = NullBackend::class;
306  $backendOptions = [];
307  }
308 
309  // Add the cache identifier to the groups that it should be attached to, or use the default ones.
310  if (isset($this->cacheConfigurations[$identifier]['groups']) && is_array($this->cacheConfigurations[$identifier]['groups'])) {
311  $assignedGroups = $this->cacheConfigurations[$identifier]['groups'];
312  } else {
313  $assignedGroups = $this->defaultCacheConfiguration['groups'];
314  }
315  foreach ($assignedGroups as $groupIdentifier) {
316  if (!isset($this->cacheGroups[$groupIdentifier])) {
317  $this->cacheGroups[$groupIdentifier] = [];
318  }
319  $this->cacheGroups[$groupIdentifier][] = $identifier;
320  }
321 
322  // New operator used on purpose: This class is required early during
323  // bootstrap before makeInstance() is properly set up
324  $backend = '\\' . ltrim($backend, '\\');
325  $backendInstance = new $backend('production', $backendOptions);
326  if (!$backendInstance instanceof BackendInterface) {
327  throw new InvalidBackendException('"' . $backend . '" is not a valid cache backend object.', 1464550977);
328  }
329  if (is_callable([$backendInstance, 'initializeObject'])) {
330  $backendInstance->initializeObject();
331  }
332 
333  // New used on purpose, see comment above
334  $frontendInstance = new $frontend($identifier, $backendInstance);
335  if (!$frontendInstance instanceof FrontendInterface) {
336  throw new InvalidCacheException('"' . $frontend . '" is not a valid cache frontend object.', 1464550984);
337  }
338  if (is_callable([$frontendInstance, 'initializeObject'])) {
339  $frontendInstance->initializeObject();
340  }
341 
342  $this->registerCache($frontendInstance);
343  }
344 
354  public function setLimbo(bool $limbo)
355  {
356  $this->limbo = $limbo;
357  }
358 }
‪TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend
Definition: TransientMemoryBackend.php:24
‪TYPO3\CMS\Core\Cache
‪TYPO3\CMS\Core\Cache\CacheManager\$cacheGroups
‪array $cacheGroups
Definition: CacheManager.php:50
‪TYPO3\CMS\Core\Cache\CacheManager\getCache
‪FrontendInterface getCache($identifier)
Definition: CacheManager.php:129
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Cache\CacheManager\registerCache
‪registerCache(FrontendInterface $cache)
Definition: CacheManager.php:113
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesInGroupByTags
‪flushCachesInGroupByTags($groupIdentifier, array $tags)
Definition: CacheManager.php:213
‪TYPO3\CMS\Core\Cache\CacheManager\createCache
‪createCache($identifier)
Definition: CacheManager.php:276
‪TYPO3
‪TYPO3\CMS\Core\Cache\CacheManager\createAllCaches
‪createAllCaches()
Definition: CacheManager.php:259
‪TYPO3\CMS\Core\Cache\Exception\InvalidBackendException
Definition: InvalidBackendException.php:21
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesByTags
‪flushCachesByTags(array $tags)
Definition: CacheManager.php:248
‪TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend
Definition: Typo3DatabaseBackend.php:30
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
Definition: NoSuchCacheException.php:21
‪TYPO3\CMS\Core\Cache\CacheManager\$cacheConfigurations
‪array $cacheConfigurations
Definition: CacheManager.php:41
‪TYPO3\CMS\Core\Cache\CacheManager\hasCache
‪bool hasCache($identifier)
Definition: CacheManager.php:146
‪TYPO3\CMS\Core\Cache\Backend\BackendInterface
Definition: BackendInterface.php:23
‪TYPO3\CMS\Core\Cache\CacheManager\$disableCaching
‪bool $disableCaching
Definition: CacheManager.php:63
‪TYPO3\CMS\Core\Cache\CacheManager\flushCaches
‪flushCaches()
Definition: CacheManager.php:154
‪TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
Definition: VariableFrontend.php:24
‪TYPO3\CMS\Core\Cache\Backend\NullBackend
Definition: NullBackend.php:21
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesInGroupByTag
‪flushCachesInGroupByTag($groupIdentifier, $tag)
Definition: CacheManager.php:189
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesInGroup
‪flushCachesInGroup($groupIdentifier)
Definition: CacheManager.php:168
‪TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException
Definition: DuplicateIdentifierException.php:21
‪TYPO3\CMS\Core\Cache\CacheManager\$limbo
‪bool $limbo
Definition: CacheManager.php:72
‪TYPO3\CMS\Core\Cache\Exception\InvalidCacheException
Definition: InvalidCacheException.php:21
‪TYPO3\CMS\Core\Cache\CacheManager\$caches
‪FrontendInterface[] $caches
Definition: CacheManager.php:37
‪TYPO3\CMS\Core\Cache\CacheManager\$defaultCacheConfiguration
‪array $defaultCacheConfiguration
Definition: CacheManager.php:54
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
Definition: NoSuchCacheGroupException.php:21
‪TYPO3\CMS\Core\Cache\CacheManager\setCacheConfigurations
‪setCacheConfigurations(array $cacheConfigurations)
Definition: CacheManager.php:97
‪TYPO3\CMS\Core\Cache\CacheManager\flushCachesByTag
‪flushCachesByTag($tag)
Definition: CacheManager.php:235
‪TYPO3\CMS\Core\Cache\CacheManager\__construct
‪__construct(bool $disableCaching=false)
Definition: CacheManager.php:77