Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / t3lib / cache / frontend / class.t3lib_cache_frontend_variablefrontend.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 * A cache for any kinds of PHP variables
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_frontend_VariableFrontend extends t3lib_cache_frontend_AbstractFrontend {
36
37 /**
38 * Saves the value of a PHP variable in the cache. Note that the variable
39 * will be serialized if necessary.
40 *
41 * @param string $entryIdentifier An identifier used for this cache entry
42 * @param mixed $variable The variable to cache
43 * @param array $tags Tags to associate with this cache entry
44 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
45 * @return void
46 * @author Robert Lemke <robert@typo3.org>
47 * @author Karsten Dambekalns <karsten@typo3.org>
48 */
49 public function set($entryIdentifier, $variable, $tags = array(), $lifetime = NULL) {
50 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
51 throw new InvalidArgumentException(
52 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
53 1233058264
54 );
55 }
56
57 foreach ($tags as $tag) {
58 if (!$this->isValidTag($tag)) {
59 throw new InvalidArgumentException(
60 '"' . $tag . '" is not a valid tag for a cache entry.',
61 1233058269
62 );
63 }
64 }
65
66 $this->backend->set($entryIdentifier, serialize($variable), $tags, $lifetime);
67 }
68
69 /**
70 * Loads a variable value from the cache.
71 *
72 * @param string Identifier of the cache entry to fetch
73 * @return mixed The value
74 * @author Robert Lemke <robert@typo3.org>
75 * @throws t3lib_cache_exception_ClassAlreadyLoaded if the class already exists
76 */
77 public function get($entryIdentifier) {
78 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
79 throw new InvalidArgumentException(
80 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
81 1233058294
82 );
83 }
84
85 return unserialize($this->backend->get($entryIdentifier));
86 }
87
88 /**
89 * Finds and returns all cache entries which are tagged by the specified tag.
90 *
91 * @param string $tag The tag to search for
92 * @return array An array with the content of all matching entries. An empty array if no entries matched
93 * @author Karsten Dambekalns <karsten@typo3.org>
94 */
95 public function getByTag($tag) {
96 if (!$this->isValidTag($tag)) {
97 throw new InvalidArgumentException(
98 '"' . $tag . '" is not a valid tag for a cache entry.',
99 1233058312
100 );
101 }
102
103 $entries = array();
104 $identifiers = $this->backend->findIdentifiersByTag($tag);
105
106 foreach ($identifiers as $identifier) {
107 $entries[] = unserialize($this->backend->get($identifier));
108 }
109
110 return $entries;
111 }
112
113 }
114
115
116 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_variablecache.php']) {
117 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_variablecache.php']);
118 }
119
120 ?>