2 /***************************************************************
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
29 * This file is a backport from FLOW3
32 * @subpackage t3lib_cache
35 class t3lib_cache_Manager
implements t3lib_Singleton
{
38 * @const Cache Entry depends on the PHP code of the packages
40 const TAG_PACKAGES_CODE
= '%PACKAGES_CODE%';
43 * @var t3lib_cache_Factory
45 protected $cacheFactory;
50 protected $caches = array();
55 protected $cacheConfigurations = array(
57 'frontend' => 't3lib_cache_frontend_VariableFrontend',
58 'backend' => 't3lib_cache_backend_FileBackend',
59 'backendOptions' => array()
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:
72 * If one of the options is not specified, the default value is assumed.
73 * Existing cache configurations are preserved.
75 * @param array The cache configurations to set
77 * @author Robert Lemke <robert@typo3.org>
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);
85 $this->cacheConfigurations
[$identifier] = $configuration;
90 * Injects the cache factory
92 * @param t3lib_cache_Factory The cache factory
94 * @author Robert Lemke <robert@typo3.org>
95 * @author Ingo Renner <ingo@typo3.org>
98 public function setCacheFactory(t3lib_cache_Factory
$cacheFactory) {
99 $this->cacheFactory
= $cacheFactory;
100 $this->cacheFactory
->setCacheManager($this);
104 * Initializes the cache manager
107 * @author Robert Lemke <robert@typo3.org>
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'];
117 $cache = $this->cacheFactory
->create($identifier, $frontend, $backend, $backendOptions);
123 * Registers a cache so it can be retrieved at a later point.
125 * @param t3lib_cache_frontend_Frontend The cache frontend to be registered
127 * @throws t3lib_cache_exception_DuplicateIdentifier if a cache with the given identifier has already been registered.
128 * @author Robert Lemke <robert@typo3.org>
130 public function registerCache(t3lib_cache_frontend_Frontend
$cache) {
131 $identifier = $cache->getIdentifier();
133 if (isset($this->caches
[$identifier])) {
134 throw new t3lib_cache_exception_DuplicateIdentifier(
135 'A cache with identifier "' . $identifier . '" has already been registered.',
140 $this->caches
[$identifier] = $cache;
144 * Returns the cache specified by $identifier
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>
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.',
159 return $this->caches
[$identifier];
163 * Checks if the specified cache has been registered.
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>
169 public function hasCache($identifier) {
170 return isset($this->caches
[$identifier]);
174 * Flushes all registered caches
177 * @author Robert Lemke <robert@typo3.org>
179 public function flushCaches() {
180 foreach ($this->caches
as $cache) {
186 * Flushes entries tagged by the specified tag of all registered
189 * @param string Tag to search for
191 * @author Robert Lemke <robert@typo3.org>
193 public function flushCachesByTag($tag) {
194 foreach ($this->caches
as $cache) {
195 $cache->flushByTag($tag);
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']);