TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
CacheManager.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Cache;
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 
27 
36 {
40  protected $caches = [];
41 
45  protected $cacheConfigurations = [];
46 
55  protected $cacheGroups = [];
56 
61  'frontend' => VariableFrontend::class,
62  'backend' => Typo3DatabaseBackend::class,
63  'options' => [],
64  'groups' => ['all']
65  ];
66 
84  {
85  foreach ($cacheConfigurations as $identifier => $configuration) {
86  if (!is_array($configuration)) {
87  throw new \InvalidArgumentException('The cache configuration for cache "' . $identifier . '" was not an array as expected.', 1231259656);
88  }
89  $this->cacheConfigurations[$identifier] = $configuration;
90  }
91  }
92 
101  public function registerCache(FrontendInterface $cache)
102  {
103  $identifier = $cache->getIdentifier();
104  if (isset($this->caches[$identifier])) {
105  throw new DuplicateIdentifierException('A cache with identifier "' . $identifier . '" has already been registered.', 1203698223);
106  }
107  $this->caches[$identifier] = $cache;
108  }
109 
118  public function getCache($identifier)
119  {
120  if ($this->hasCache($identifier) === false) {
121  throw new NoSuchCacheException('A cache with identifier "' . $identifier . '" does not exist.', 1203699034);
122  }
123  if (!isset($this->caches[$identifier])) {
124  $this->createCache($identifier);
125  }
126  return $this->caches[$identifier];
127  }
128 
136  public function hasCache($identifier)
137  {
138  return isset($this->caches[$identifier]) || isset($this->cacheConfigurations[$identifier]);
139  }
140 
147  public function flushCaches()
148  {
149  $this->createAllCaches();
150  foreach ($this->caches as $cache) {
151  $cache->flush();
152  }
153  }
154 
163  public function flushCachesInGroup($groupIdentifier)
164  {
165  $this->createAllCaches();
166  if (!isset($this->cacheGroups[$groupIdentifier])) {
167  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390334120);
168  }
169  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
170  if (isset($this->caches[$cacheIdentifier])) {
171  $this->caches[$cacheIdentifier]->flush();
172  }
173  }
174  }
175 
186  public function flushCachesInGroupByTag($groupIdentifier, $tag)
187  {
188  if (empty($tag)) {
189  return;
190  }
191  $this->createAllCaches();
192  if (!isset($this->cacheGroups[$groupIdentifier])) {
193  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390337129);
194  }
195  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
196  if (isset($this->caches[$cacheIdentifier])) {
197  $this->caches[$cacheIdentifier]->flushByTag($tag);
198  }
199  }
200  }
201 
212  public function flushCachesInGroupByTags($groupIdentifier, array $tags)
213  {
214  if (empty($tag)) {
215  return;
216  }
217  $this->createAllCaches();
218  if (!isset($this->cacheGroups[$groupIdentifier])) {
219  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390337130);
220  }
221  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
222  if (isset($this->caches[$cacheIdentifier])) {
223  $this->caches[$cacheIdentifier]->flushByTags($tags);
224  }
225  }
226  }
227 
236  public function flushCachesByTag($tag)
237  {
238  $this->createAllCaches();
239  foreach ($this->caches as $cache) {
240  $cache->flushByTag($tag);
241  }
242  }
243 
251  public function flushCachesByTags(array $tags)
252  {
253  $this->createAllCaches();
254  foreach ($this->caches as $cache) {
255  $cache->flushByTags($tags);
256  }
257  }
258 
264  protected function createAllCaches()
265  {
266  foreach ($this->cacheConfigurations as $identifier => $_) {
267  if (!isset($this->caches[$identifier])) {
268  $this->createCache($identifier);
269  }
270  }
271  }
272 
281  protected function createCache($identifier)
282  {
283  if (isset($this->cacheConfigurations[$identifier]['frontend'])) {
284  $frontend = $this->cacheConfigurations[$identifier]['frontend'];
285  } else {
286  $frontend = $this->defaultCacheConfiguration['frontend'];
287  }
288  if (isset($this->cacheConfigurations[$identifier]['backend'])) {
289  $backend = $this->cacheConfigurations[$identifier]['backend'];
290  } else {
291  $backend = $this->defaultCacheConfiguration['backend'];
292  }
293  if (isset($this->cacheConfigurations[$identifier]['options'])) {
294  $backendOptions = $this->cacheConfigurations[$identifier]['options'];
295  } else {
296  $backendOptions = $this->defaultCacheConfiguration['options'];
297  }
298 
299  // Add the cache identifier to the groups that it should be attached to, or use the default ones.
300  if (isset($this->cacheConfigurations[$identifier]['groups']) && is_array($this->cacheConfigurations[$identifier]['groups'])) {
301  $assignedGroups = $this->cacheConfigurations[$identifier]['groups'];
302  } else {
303  $assignedGroups = $this->defaultCacheConfiguration['groups'];
304  }
305  foreach ($assignedGroups as $groupIdentifier) {
306  if (!isset($this->cacheGroups[$groupIdentifier])) {
307  $this->cacheGroups[$groupIdentifier] = [];
308  }
309  $this->cacheGroups[$groupIdentifier][] = $identifier;
310  }
311 
312  // New operator used on purpose: This class is required early during
313  // bootstrap before makeInstance() is properly set up
314  $backend = '\\' . ltrim($backend, '\\');
315  $backendInstance = new $backend('production', $backendOptions);
316  if (!$backendInstance instanceof BackendInterface) {
317  throw new InvalidBackendException('"' . $backend . '" is not a valid cache backend object.', 1464550977);
318  }
319  if (is_callable([$backendInstance, 'initializeObject'])) {
320  $backendInstance->initializeObject();
321  }
322 
323  // New used on purpose, see comment above
324  $frontendInstance = new $frontend($identifier, $backendInstance);
325  if (!$frontendInstance instanceof FrontendInterface) {
326  throw new InvalidCacheException('"' . $frontend . '" is not a valid cache frontend object.', 1464550984);
327  }
328  if (is_callable([$frontendInstance, 'initializeObject'])) {
329  $frontendInstance->initializeObject();
330  }
331 
332  $this->registerCache($frontendInstance);
333  }
334 }
registerCache(FrontendInterface $cache)
setCacheConfigurations(array $cacheConfigurations)
flushCachesInGroupByTag($groupIdentifier, $tag)
flushCachesInGroup($groupIdentifier)
flushCachesInGroupByTags($groupIdentifier, array $tags)