TYPO3 API  SVNRelease
class.t3lib_cache_frontend_variablefrontend.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 cache for any kinds of PHP variables
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_frontend_variablefrontend.php 10121 2011-01-18 20:15:30Z ohader $
00035  */
00036 class t3lib_cache_frontend_VariableFrontend extends t3lib_cache_frontend_AbstractFrontend {
00037 
00038     /**
00039      * If the extension "igbinary" is installed, use it for increased performance
00040      *
00041      * @var boolean
00042      */
00043     protected $useIgBinary = FALSE;
00044 
00045     /**
00046      * Constructs the cache
00047      *
00048      * @param string A identifier which describes this cache
00049      * @param t3lib_cache_backend_Backend Backend to be used for this cache
00050      * @author Robert Lemke <robert@typo3.org>
00051      * @throws InvalidArgumentException if the identifier doesn't match PATTERN_ENTRYIDENTIFIER
00052      * @internal
00053      */
00054     public function __construct($identifier, t3lib_cache_backend_Backend $backend) {
00055         parent::__construct($identifier, $backend);
00056         $this->initializeObject();
00057     }
00058 
00059     /**
00060      * Initializes this cache frontend
00061      *
00062      * @return void
00063      * @author Robert Lemke <robert@typo3.org>
00064      */
00065     protected function initializeObject() {
00066         $this->useIgBinary = extension_loaded('igbinary');
00067     }
00068 
00069     /**
00070      * Saves the value of a PHP variable in the cache. Note that the variable
00071      * will be serialized if necessary.
00072      *
00073      * @param string $entryIdentifier An identifier used for this cache entry
00074      * @param mixed $variable The variable to cache
00075      * @param array $tags Tags to associate with this cache entry
00076      * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
00077      * @return void
00078      * @author Robert Lemke <robert@typo3.org>
00079      * @author Karsten Dambekalns <karsten@typo3.org>
00080      */
00081     public function set($entryIdentifier, $variable, $tags = array(), $lifetime = NULL) {
00082         if (!$this->isValidEntryIdentifier($entryIdentifier)) {
00083             throw new InvalidArgumentException(
00084                 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
00085                 1233058264
00086             );
00087         }
00088 
00089         foreach ($tags as $tag) {
00090             if (!$this->isValidTag($tag)) {
00091                 throw new InvalidArgumentException(
00092                     '"' . $tag . '" is not a valid tag for a cache entry.',
00093                     1233058269
00094                 );
00095             }
00096         }
00097 
00098         if ($this->useIgBinary === TRUE) {
00099             $this->backend->set($entryIdentifier, igbinary_serialize($variable), $tags, $lifetime);
00100         } else {
00101             $this->backend->set($entryIdentifier, serialize($variable), $tags, $lifetime);
00102         }
00103     }
00104 
00105     /**
00106      * Loads a variable value from the cache.
00107      *
00108      * @param string Identifier of the cache entry to fetch
00109      * @return mixed The value
00110      * @author Robert Lemke <robert@typo3.org>
00111      * @throws t3lib_cache_exception_ClassAlreadyLoaded if the class already exists
00112      */
00113     public function get($entryIdentifier) {
00114         if (!$this->isValidEntryIdentifier($entryIdentifier)) {
00115             throw new InvalidArgumentException(
00116                 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
00117                 1233058294
00118             );
00119         }
00120 
00121         return ($this->useIgBinary === TRUE) ? igbinary_unserialize($this->backend->get($entryIdentifier)) : unserialize($this->backend->get($entryIdentifier));
00122     }
00123 
00124     /**
00125      * Finds and returns all cache entries which are tagged by the specified tag.
00126      *
00127      * @param string $tag The tag to search for
00128      * @return array An array with the content of all matching entries. An empty array if no entries matched
00129      * @author Karsten Dambekalns <karsten@typo3.org>
00130      */
00131     public function getByTag($tag) {
00132         if (!$this->isValidTag($tag)) {
00133             throw new InvalidArgumentException(
00134                 '"' . $tag . '" is not a valid tag for a cache entry.',
00135                 1233058312
00136             );
00137         }
00138 
00139         $entries = array();
00140         $identifiers = $this->backend->findIdentifiersByTag($tag);
00141 
00142         foreach ($identifiers as $identifier) {
00143             $entries[] = ($this->useIgBinary === TRUE) ? igbinary_unserialize($this->backend->get($identifier)) : unserialize($this->backend->get($identifier));
00144         }
00145 
00146         return $entries;
00147     }
00148 
00149 }
00150 
00151 
00152 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_variablecache.php'])) {
00153     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_variablecache.php']);
00154 }
00155 
00156 ?>