[TASK] Remove leftover FLOW3 code in Caching
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / CacheManager.php
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
17 use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException;
18
19 /**
20 * The Cache Manager
21 *
22 * This file is a backport from FLOW3
23 * @scope singleton
24 * @api
25 */
26 class CacheManager implements \TYPO3\CMS\Core\SingletonInterface
27 {
28 /**
29 * @var \TYPO3\CMS\Core\Cache\CacheFactory
30 */
31 protected $cacheFactory;
32
33 /**
34 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface[]
35 */
36 protected $caches = array();
37
38 /**
39 * @var array
40 */
41 protected $cacheConfigurations = array();
42
43 /**
44 * Used to flush caches of a specific group
45 * is an associative array containing the group identifier as key
46 * and the identifier as an array within that group
47 * groups are set via the cache configurations of each cache.
48 *
49 * @var array
50 */
51 protected $cacheGroups = array();
52
53 /**
54 * @var array Default cache configuration as fallback
55 */
56 protected $defaultCacheConfiguration = array(
57 'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
58 'backend' => \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class,
59 'options' => array(),
60 'groups' => array('all')
61 );
62
63 /**
64 * @param \TYPO3\CMS\Core\Cache\CacheFactory $cacheFactory
65 * @return void
66 */
67 public function injectCacheFactory(\TYPO3\CMS\Core\Cache\CacheFactory $cacheFactory)
68 {
69 $this->cacheFactory = $cacheFactory;
70 }
71
72 /**
73 * Sets configurations for caches. The key of each entry specifies the
74 * cache identifier and the value is an array of configuration options.
75 * Possible options are:
76 *
77 * frontend
78 * backend
79 * backendOptions
80 *
81 * If one of the options is not specified, the default value is assumed.
82 * Existing cache configurations are preserved.
83 *
84 * @param array $cacheConfigurations The cache configurations to set
85 * @return void
86 * @throws \InvalidArgumentException If $cacheConfigurations is not an array
87 */
88 public function setCacheConfigurations(array $cacheConfigurations)
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
98 /**
99 * Registers a cache so it can be retrieved at a later point.
100 *
101 * @param \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache The cache frontend to be registered
102 * @return void
103 * @throws \TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException if a cache with the given identifier has already been registered.
104 * @api
105 */
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
115 /**
116 * Returns the cache specified by $identifier
117 *
118 * @param string $identifier Identifies which cache to return
119 * @return \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface The specified cache frontend
120 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
121 * @api
122 */
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
134 /**
135 * Checks if the specified cache has been registered.
136 *
137 * @param string $identifier The identifier of the cache
138 * @return bool TRUE if a cache with the given identifier exists, otherwise FALSE
139 * @api
140 */
141 public function hasCache($identifier)
142 {
143 return isset($this->caches[$identifier]) || isset($this->cacheConfigurations[$identifier]);
144 }
145
146 /**
147 * Flushes all registered caches
148 *
149 * @return void
150 * @api
151 */
152 public function flushCaches()
153 {
154 $this->createAllCaches();
155 foreach ($this->caches as $cache) {
156 $cache->flush();
157 }
158 }
159
160 /**
161 * Flushes all registered caches of a specific group
162 *
163 * @param string $groupIdentifier
164 * @return void
165 * @throws NoSuchCacheGroupException
166 * @api
167 */
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
182 /**
183 * Flushes entries tagged by the specified tag of all registered
184 * caches of a specific group.
185 *
186 * @param string $groupIdentifier
187 * @param string $tag Tag to search for
188 * @return void
189 * @throws NoSuchCacheGroupException
190 * @api
191 */
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
206 /**
207 * Flushes entries tagged by the specified tag of all registered
208 * caches.
209 *
210 * @param string $tag Tag to search for
211 * @return void
212 * @api
213 */
214 public function flushCachesByTag($tag)
215 {
216 $this->createAllCaches();
217 foreach ($this->caches as $cache) {
218 $cache->flushByTag($tag);
219 }
220 }
221
222 /**
223 * Instantiates all registered caches.
224 *
225 * @return void
226 */
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
236 /**
237 * Instantiates the cache for $identifier.
238 *
239 * @param string $identifier
240 * @return void
241 */
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] = array();
269 }
270 $this->cacheGroups[$groupIdentifier][] = $identifier;
271 }
272
273 $this->cacheFactory->create($identifier, $frontend, $backend, $backendOptions);
274 }
275 }