Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / t3lib / cache / class.t3lib_cache_manager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * The Cache Manager
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @version $Id$
34 */
35 class t3lib_cache_Manager implements t3lib_Singleton {
36
37 /**
38 * @const Cache Entry depends on the PHP code of the packages
39 */
40 const TAG_PACKAGES_CODE = '%PACKAGES_CODE%';
41
42 /**
43 * @var t3lib_cache_Factory
44 */
45 protected $cacheFactory;
46
47 /**
48 * @var array
49 */
50 protected $caches = array();
51
52 /**
53 * @var array
54 */
55 protected $cacheConfigurations = array(
56 'default' => array(
57 'frontend' => 't3lib_cache_frontend_VariableFrontend',
58 'backend' => 't3lib_cache_backend_FileBackend',
59 'backendOptions' => array()
60 )
61 );
62
63 /**
64 * Sets configurations for caches. The key of each entry specifies the
65 * cache identifier and the value is an array of configuration options.
66 * Possible options are:
67 *
68 * frontend
69 * backend
70 * backendOptions
71 *
72 * If one of the options is not specified, the default value is assumed.
73 * Existing cache configurations are preserved.
74 *
75 * @param array The cache configurations to set
76 * @return void
77 * @author Robert Lemke <robert@typo3.org>
78 * @internal
79 */
80 public function setCacheConfigurations(array $cacheConfigurations) {
81 foreach ($cacheConfigurations as $identifier => $configuration) {
82 if (!is_array($configuration)) {
83 throw new InvalidArgumentException('The cache configuration for cache "' . $identifier . '" was not an array as expected.', 1235838075);
84 }
85 $this->cacheConfigurations[$identifier] = $configuration;
86 }
87 }
88
89 /**
90 * Injects the cache factory
91 *
92 * @param t3lib_cache_Factory The cache factory
93 * @return void
94 * @author Robert Lemke <robert@typo3.org>
95 * @author Ingo Renner <ingo@typo3.org>
96 * @internal
97 */
98 public function setCacheFactory(t3lib_cache_Factory $cacheFactory) {
99 $this->cacheFactory = $cacheFactory;
100 $this->cacheFactory->setCacheManager($this);
101 }
102
103 /**
104 * Initializes the cache manager
105 *
106 * @return void
107 * @author Robert Lemke <robert@typo3.org>
108 * @internal
109 */
110 public function initialize() {
111 foreach ($this->cacheConfigurations as $identifier => $configuration) {
112 if ($identifier !== 'default') {
113 $frontend = isset($configuration['frontend']) ? $configuration['frontend'] : $this->cacheConfigurations['default']['frontend'];
114 $backend = isset($configuration['backend']) ? $configuration['backend'] : $this->cacheConfigurations['default']['backend'];
115 $backendOptions = isset($configuration['backendOptions']) ? $configuration['backendOptions'] : $this->cacheConfigurations['default']['backendOptions'];
116
117 $cache = $this->cacheFactory->create($identifier, $frontend, $backend, $backendOptions);
118 }
119 }
120 }
121
122 /**
123 * Registers a cache so it can be retrieved at a later point.
124 *
125 * @param t3lib_cache_frontend_Frontend The cache frontend to be registered
126 * @return void
127 * @throws t3lib_cache_exception_DuplicateIdentifier if a cache with the given identifier has already been registered.
128 * @author Robert Lemke <robert@typo3.org>
129 */
130 public function registerCache(t3lib_cache_frontend_Frontend $cache) {
131 $identifier = $cache->getIdentifier();
132
133 if (isset($this->caches[$identifier])) {
134 throw new t3lib_cache_exception_DuplicateIdentifier(
135 'A cache with identifier "' . $identifier . '" has already been registered.',
136 1203698223
137 );
138 }
139
140 $this->caches[$identifier] = $cache;
141 }
142
143 /**
144 * Returns the cache specified by $identifier
145 *
146 * @param string Identifies which cache to return
147 * @return t3lib_cache_frontend_Cache The specified cache frontend
148 * @throws t3lib_cache_exception_NoSuchCache
149 * @author Robert Lemke <robert@typo3.org>
150 */
151 public function getCache($identifier) {
152 if (!isset($this->caches[$identifier])) {
153 throw new t3lib_cache_exception_NoSuchCache(
154 'A cache with identifier "' . $identifier . '" does not exist.',
155 1203699034
156 );
157 }
158
159 return $this->caches[$identifier];
160 }
161
162 /**
163 * Checks if the specified cache has been registered.
164 *
165 * @param string The identifier of the cache
166 * @return boolean TRUE if a cache with the given identifier exists, otherwise FALSE
167 * @author Robert Lemke <robert@typo3.org>
168 */
169 public function hasCache($identifier) {
170 return isset($this->caches[$identifier]);
171 }
172
173 /**
174 * Flushes all registered caches
175 *
176 * @return void
177 * @author Robert Lemke <robert@typo3.org>
178 */
179 public function flushCaches() {
180 foreach ($this->caches as $cache) {
181 $cache->flush();
182 }
183 }
184
185 /**
186 * Flushes entries tagged by the specified tag of all registered
187 * caches.
188 *
189 * @param string Tag to search for
190 * @return void
191 * @author Robert Lemke <robert@typo3.org>
192 */
193 public function flushCachesByTag($tag) {
194 foreach ($this->caches as $cache) {
195 $cache->flushByTag($tag);
196 }
197 }
198 }
199
200
201 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_manager.php']) {
202 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_manager.php']);
203 }
204
205 ?>