TYPO3 API  SVNRelease
class.t3lib_cache_backend_transientmemorybackend.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  * A caching backend which stores cache entries during one script run.
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_backend_transientmemorybackend.php 10121 2011-01-18 20:15:30Z ohader $
00035  */
00036 class t3lib_cache_backend_TransientMemoryBackend extends t3lib_cache_backend_AbstractBackend {
00037 
00038     /**
00039      * @var array
00040      */
00041     protected $entries = array();
00042 
00043     /**
00044      * @var array
00045      */
00046     protected $tagsAndEntries = array();
00047 
00048     /**
00049      * Saves data in the cache.
00050      *
00051      * @param string $entryIdentifier An identifier for this specific cache entry
00052      * @param string $data The data to be stored
00053      * @param array $tags Tags to associate with this cache entry
00054      * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
00055      * @return void
00056      * @throws t3lib_cache_Exception if no cache frontend has been set.
00057      * @author Robert Lemke <robert@typo3.org>
00058      */
00059     public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
00060         if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
00061             throw new t3lib_cache_Exception('No cache frontend has been set yet via setCache().', 1238244992);
00062         }
00063         if (!is_string($data)) {
00064             throw new t3lib_cache_exception_InvalidData('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1238244993);
00065         }
00066         $this->entries[$entryIdentifier] = $data;
00067         foreach ($tags as $tag) {
00068             $this->tagsAndEntries[$tag][$entryIdentifier] = TRUE;
00069         }
00070     }
00071 
00072     /**
00073      * Loads data from the cache.
00074      *
00075      * @param string $entryIdentifier An identifier which describes the cache entry to load
00076      * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
00077      * @author Robert Lemke <robert@typo3.org>
00078      */
00079     public function get($entryIdentifier) {
00080         return (isset($this->entries[$entryIdentifier])) ? $this->entries[$entryIdentifier] : FALSE;
00081     }
00082 
00083     /**
00084      * Checks if a cache entry with the specified identifier exists.
00085      *
00086      * @param string $entryIdentifier An identifier specifying the cache entry
00087      * @return boolean TRUE if such an entry exists, FALSE if not
00088      * @author Robert Lemke <robert@typo3.org>
00089      */
00090     public function has($entryIdentifier) {
00091         return isset($this->entries[$entryIdentifier]);
00092     }
00093 
00094     /**
00095      * Removes all cache entries matching the specified identifier.
00096      *
00097      * @param string $entryIdentifier Specifies the cache entry to remove
00098      * @return boolean TRUE if the entry could be removed or FALSE if no entry was found
00099      * @author Robert Lemke <robert@typo3.org>
00100      */
00101     public function remove($entryIdentifier) {
00102         if (isset($this->entries[$entryIdentifier])) {
00103             unset($this->entries[$entryIdentifier]);
00104             foreach (array_keys($this->tagsAndEntries) as $tag) {
00105                 if (isset($this->tagsAndEntries[$tag][$entryIdentifier])) {
00106                     unset ($this->tagsAndEntries[$tag][$entryIdentifier]);
00107                 }
00108             }
00109             return TRUE;
00110         } else {
00111             return FALSE;
00112         }
00113     }
00114 
00115     /**
00116      * Finds and returns all cache entry identifiers which are tagged by the
00117      * specified tag.
00118      *
00119      * @param string $tag The tag to search for
00120      * @return array An array with identifiers of all matching entries. An empty array if no entries matched
00121      * @author Robert Lemke <robert@typo3.org>
00122      */
00123     public function findIdentifiersByTag($tag) {
00124         if (isset($this->tagsAndEntries[$tag])) {
00125             return array_keys($this->tagsAndEntries[$tag]);
00126         } else {
00127             return array();
00128         }
00129     }
00130 
00131     /**
00132      * Finds and returns all cache entry identifiers which are tagged by the
00133      * specified tags.
00134      * The asterisk ("*") is allowed as a wildcard at the beginning and the end
00135      * of a tag.
00136      *
00137      * @param array Array of tags to search for, the "*" wildcard is supported
00138      * @return array An array with identifiers of all matching entries. An empty array if no entries matched
00139      * @author  Ingo Renner <ingo@typo3.org>
00140      */
00141     public function findIdentifiersByTags(array $tags) {
00142         $taggedEntries = array();
00143         $foundEntries = array();
00144 
00145         foreach ($tags as $tag) {
00146             $taggedEntries[$tag] = $this->findIdentifiersByTag($tag);
00147         }
00148 
00149         $intersectedTaggedEntries = call_user_func_array('array_intersect', $taggedEntries);
00150 
00151         foreach ($intersectedTaggedEntries as $entryIdentifier) {
00152             $foundEntries[$entryIdentifier] = $entryIdentifier;
00153         }
00154 
00155         return $foundEntries;
00156     }
00157 
00158     /**
00159      * Removes all cache entries of this cache.
00160      *
00161      * @return void
00162      * @author Robert Lemke <robert@typo3.org>
00163      */
00164     public function flush() {
00165         $this->entries = array();
00166         $this->tagsAndEntries = array();
00167     }
00168 
00169     /**
00170      * Removes all cache entries of this cache which are tagged by the specified tag.
00171      *
00172      * @param string $tag The tag the entries must have
00173      * @return void
00174      * @author Robert Lemke <robert@typo3.org>
00175      */
00176     public function flushByTag($tag) {
00177         $identifiers = $this->findIdentifiersByTag($tag);
00178         foreach ($identifiers as $identifier) {
00179             $this->remove($identifier);
00180         }
00181     }
00182 
00183     /**
00184      * Removes all cache entries of this cache which are tagged by the specified tags.
00185      *
00186      * @param   array   The tags the entries must have
00187      * @return void
00188      * @author  Ingo Renner <ingo@typo3.org>
00189      */
00190     public function flushByTags(array $tags) {
00191         foreach ($tags as $tag) {
00192             $this->flushByTag($tag);
00193         }
00194     }
00195 
00196     /**
00197      * Does nothing
00198      *
00199      * @return void
00200      * @author Robert Lemke <robert@typo3.org>
00201      */
00202     public function collectGarbage() {
00203     }
00204 }
00205 
00206 
00207 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_transientmemorybackend.php'])) {
00208     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_transientmemorybackend.php']);
00209 }
00210 
00211 ?>