TYPO3 CMS  TYPO3_7-6
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 
18 
27 {
31  protected $cacheFactory;
32 
36  protected $caches = [];
37 
41  protected $cacheConfigurations = [];
42 
51  protected $cacheGroups = [];
52 
57  'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
58  'backend' => \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class,
59  'options' => [],
60  'groups' => ['all']
61  ];
62 
67  public function injectCacheFactory(\TYPO3\CMS\Core\Cache\CacheFactory $cacheFactory)
68  {
69  $this->cacheFactory = $cacheFactory;
70  }
71 
89  {
90  foreach ($cacheConfigurations as $identifier => $configuration) {
91  if (!is_array($configuration)) {
92  throw new \InvalidArgumentException('The cache configuration for cache "' . $identifier . '" was not an array as expected.', 1231259656);
93  }
94  $this->cacheConfigurations[$identifier] = $configuration;
95  }
96  }
97 
106  public function registerCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache)
107  {
108  $identifier = $cache->getIdentifier();
109  if (isset($this->caches[$identifier])) {
110  throw new \TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException('A cache with identifier "' . $identifier . '" has already been registered.', 1203698223);
111  }
112  $this->caches[$identifier] = $cache;
113  }
114 
123  public function getCache($identifier)
124  {
125  if ($this->hasCache($identifier) === false) {
126  throw new \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException('A cache with identifier "' . $identifier . '" does not exist.', 1203699034);
127  }
128  if (!isset($this->caches[$identifier])) {
129  $this->createCache($identifier);
130  }
131  return $this->caches[$identifier];
132  }
133 
141  public function hasCache($identifier)
142  {
143  return isset($this->caches[$identifier]) || isset($this->cacheConfigurations[$identifier]);
144  }
145 
152  public function flushCaches()
153  {
154  $this->createAllCaches();
155  foreach ($this->caches as $cache) {
156  $cache->flush();
157  }
158  }
159 
168  public function flushCachesInGroup($groupIdentifier)
169  {
170  $this->createAllCaches();
171  if (isset($this->cacheGroups[$groupIdentifier])) {
172  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
173  if (isset($this->caches[$cacheIdentifier])) {
174  $this->caches[$cacheIdentifier]->flush();
175  }
176  }
177  } else {
178  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390334120);
179  }
180  }
181 
192  public function flushCachesInGroupByTag($groupIdentifier, $tag)
193  {
194  $this->createAllCaches();
195  if (isset($this->cacheGroups[$groupIdentifier])) {
196  foreach ($this->cacheGroups[$groupIdentifier] as $cacheIdentifier) {
197  if (isset($this->caches[$cacheIdentifier])) {
198  $this->caches[$cacheIdentifier]->flushByTag($tag);
199  }
200  }
201  } else {
202  throw new NoSuchCacheGroupException('No cache in the specified group \'' . $groupIdentifier . '\'', 1390337129);
203  }
204  }
205 
214  public function flushCachesByTag($tag)
215  {
216  $this->createAllCaches();
217  foreach ($this->caches as $cache) {
218  $cache->flushByTag($tag);
219  }
220  }
221 
227  protected function createAllCaches()
228  {
229  foreach ($this->cacheConfigurations as $identifier => $_) {
230  if (!isset($this->caches[$identifier])) {
231  $this->createCache($identifier);
232  }
233  }
234  }
235 
242  protected function createCache($identifier)
243  {
244  if (isset($this->cacheConfigurations[$identifier]['frontend'])) {
245  $frontend = $this->cacheConfigurations[$identifier]['frontend'];
246  } else {
247  $frontend = $this->defaultCacheConfiguration['frontend'];
248  }
249  if (isset($this->cacheConfigurations[$identifier]['backend'])) {
250  $backend = $this->cacheConfigurations[$identifier]['backend'];
251  } else {
252  $backend = $this->defaultCacheConfiguration['backend'];
253  }
254  if (isset($this->cacheConfigurations[$identifier]['options'])) {
255  $backendOptions = $this->cacheConfigurations[$identifier]['options'];
256  } else {
257  $backendOptions = $this->defaultCacheConfiguration['options'];
258  }
259 
260  // Add the cache identifier to the groups that it should be attached to, or use the default ones.
261  if (isset($this->cacheConfigurations[$identifier]['groups']) && is_array($this->cacheConfigurations[$identifier]['groups'])) {
262  $assignedGroups = $this->cacheConfigurations[$identifier]['groups'];
263  } else {
264  $assignedGroups = $this->defaultCacheConfiguration['groups'];
265  }
266  foreach ($assignedGroups as $groupIdentifier) {
267  if (!isset($this->cacheGroups[$groupIdentifier])) {
268  $this->cacheGroups[$groupIdentifier] = [];
269  }
270  $this->cacheGroups[$groupIdentifier][] = $identifier;
271  }
272 
273  $this->cacheFactory->create($identifier, $frontend, $backend, $backendOptions);
274  }
275 }
setCacheConfigurations(array $cacheConfigurations)
flushCachesInGroup($groupIdentifier)
registerCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache)
injectCacheFactory(\TYPO3\CMS\Core\Cache\CacheFactory $cacheFactory)
flushCachesInGroupByTag($groupIdentifier, $tag)