TYPO3 API  SVNRelease
class.t3lib_cache_manager.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2009-2011 Ingo Renner <ingo@typo3.org>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *
00017  *  This script is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU General Public License for more details.
00021  *
00022  *  This copyright notice MUST APPEAR in all copies of the script!
00023  ***************************************************************/
00024 
00025 
00026 /**
00027  * The Cache Manager
00028  *
00029  * This file is a backport from FLOW3
00030  *
00031  * @package TYPO3
00032  * @subpackage t3lib_cache
00033  * @api
00034  * @version $Id: class.t3lib_cache_manager.php 10121 2011-01-18 20:15:30Z ohader $
00035  */
00036 class t3lib_cache_Manager implements t3lib_Singleton {
00037     /**
00038      * @var t3lib_cache_Factory
00039      */
00040     protected $cacheFactory;
00041 
00042     /**
00043      * @var array
00044      */
00045     protected $caches = array();
00046 
00047     /**
00048      * @var array
00049      */
00050     protected $cacheConfigurations = array();
00051 
00052     /**
00053      * Sets configurations for caches. The key of each entry specifies the
00054      * cache identifier and the value is an array of configuration options.
00055      * Possible options are:
00056      *
00057      *   frontend
00058      *   backend
00059      *   backendOptions
00060      *
00061      * If one of the options is not specified, the default value is assumed.
00062      * Existing cache configurations are preserved.
00063      *
00064      * @param   array   The cache configurations to set
00065      * @return  void
00066      * @author  Robert Lemke <robert@typo3.org>
00067      * @internal
00068      */
00069     public function setCacheConfigurations(array $cacheConfigurations) {
00070         foreach ($cacheConfigurations as $identifier => $configuration) {
00071             if (!is_array($configuration)) {
00072                 throw new InvalidArgumentException('The cache configuration for cache "' . $identifier . '" was not an array as expected.', 1235838075);
00073             }
00074             $this->cacheConfigurations[$identifier] = $configuration;
00075         }
00076     }
00077 
00078     /**
00079      * Injects the cache factory
00080      *
00081      * @param   t3lib_cache_Factory The cache factory
00082      * @return void
00083      * @author Robert Lemke <robert@typo3.org>
00084      * @author Ingo Renner <ingo@typo3.org>
00085      * @internal
00086      */
00087     public function setCacheFactory(t3lib_cache_Factory $cacheFactory) {
00088         $this->cacheFactory = $cacheFactory;
00089         $this->cacheFactory->setCacheManager($this);
00090     }
00091 
00092     /**
00093      * Initializes the cache manager
00094      *
00095      * @return void
00096      * @author Robert Lemke <robert@typo3.org>
00097      * @internal
00098      */
00099     public function initialize() {
00100         foreach ($this->cacheConfigurations as $identifier => $configuration) {
00101             $this->cacheFactory->create(
00102                 $identifier,
00103                 $configuration['frontend'],
00104                 $configuration['backend'],
00105                 $configuration['backendOptions']
00106             );
00107         }
00108     }
00109 
00110     /**
00111      * Registers a cache so it can be retrieved at a later point.
00112      *
00113      * @param t3lib_cache_frontend_Frontend The cache frontend to be registered
00114      * @return void
00115      * @throws t3lib_cache_exception_DuplicateIdentifier if a cache with the given identifier has already been registered.
00116      * @author Robert Lemke <robert@typo3.org>
00117      */
00118     public function registerCache(t3lib_cache_frontend_Frontend $cache) {
00119         $identifier = $cache->getIdentifier();
00120 
00121         if (isset($this->caches[$identifier])) {
00122             throw new t3lib_cache_exception_DuplicateIdentifier(
00123                 'A cache with identifier "' . $identifier . '" has already been registered.',
00124                 1203698223
00125             );
00126         }
00127 
00128         $this->caches[$identifier] = $cache;
00129     }
00130 
00131     /**
00132      * Returns the cache specified by $identifier
00133      *
00134      * @param string Identifies which cache to return
00135      * @return t3lib_cache_frontend_Cache The specified cache frontend
00136      * @throws t3lib_cache_exception_NoSuchCache
00137      * @author Robert Lemke <robert@typo3.org>
00138      */
00139     public function getCache($identifier) {
00140         if (!isset($this->caches[$identifier])) {
00141             throw new t3lib_cache_exception_NoSuchCache(
00142                 'A cache with identifier "' . $identifier . '" does not exist.',
00143                 1203699034
00144             );
00145         }
00146 
00147         return $this->caches[$identifier];
00148     }
00149 
00150     /**
00151      * Checks if the specified cache has been registered.
00152      *
00153      * @param string The identifier of the cache
00154      * @return boolean TRUE if a cache with the given identifier exists, otherwise FALSE
00155      * @author Robert Lemke <robert@typo3.org>
00156      */
00157     public function hasCache($identifier) {
00158         return isset($this->caches[$identifier]);
00159     }
00160 
00161     /**
00162      * Flushes all registered caches
00163      *
00164      * @return void
00165      * @author Robert Lemke <robert@typo3.org>
00166      */
00167     public function flushCaches() {
00168         foreach ($this->caches as $cache) {
00169             $cache->flush();
00170         }
00171     }
00172 
00173     /**
00174      * Flushes entries tagged by the specified tag of all registered
00175      * caches.
00176      *
00177      * @param string Tag to search for
00178      * @return void
00179      * @author Robert Lemke <robert@typo3.org>
00180      */
00181     public function flushCachesByTag($tag) {
00182         foreach ($this->caches as $cache) {
00183             $cache->flushByTag($tag);
00184         }
00185     }
00186 }
00187 
00188 
00189 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_manager.php'])) {
00190     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_manager.php']);
00191 }
00192 
00193 ?>