Added feature 9097: Introduce a new caching framework (backported from FLOW3)
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_globals.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 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 caching backend which saves it's data in $GLOBALS - a very short living cache, probably useful only during page rendering
28 *
29 * @package TYPO3
30 * @subpackage t3lib_cache
31 * @version $Id$
32 */
33 class t3lib_cache_backend_Globals extends t3lib_cache_AbstractBackend {
34
35 /**
36 * Constructs this backend
37 *
38 * @param mixed Configuration options - depends on the actual backend
39 */
40 public function __construct(array $options = array()) {
41 parent::__construct($options);
42
43 if (!isset($GLOBALS['typo3CacheStorage'])) {
44 $GLOBALS['typo3CacheStorage'] = array();
45 }
46
47 if (!is_object($this->cache)) {
48 throw new t3lib_cache_Exception(
49 'No cache frontend has been set yet via setCache().',
50 1217611408
51 );
52 }
53
54 $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()] = array(
55 'data' => array(),
56 'tags' => array()
57 );
58 }
59
60 /**
61 * Saves data in a cache file.
62 *
63 * @param string An identifier for this specific cache entry
64 * @param string The data to be stored
65 * @param array Tags to associate with this cache entry
66 * @param integer Ignored as $GLOBALS lasts for the time of the script execution only anyway
67 * @return void
68 * @throws t3lib_cache_Exception if the directory does not exist or is not writable, or if no cache frontend has been set.
69 * @author Ingo Renner <ingo@typo3.org>
70 */
71 public function save($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
72 if (!self::isValidEntryIdentifier($entryIdentifier)) {
73 throw new InvalidArgumentException(
74 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
75 1217611184
76 );
77 }
78
79 if (!is_object($this->cache)) {
80 throw new t3lib_cache_Exception(
81 'No cache frontend has been set yet via setCache().',
82 1217611191
83 );
84 }
85
86 if (!is_string($data)) {
87 throw new t3lib_cache_Exception_InvalidData(
88 'The specified data is of type "' . gettype($data) . '" but a string is expected.',
89 1217611199
90 );
91 }
92
93 foreach ($tags as $tag) {
94 if (!self::isValidTag($tag)) {
95 throw new InvalidArgumentException(
96 '"' . $tag . '" is not a valid tag for a cache entry.',
97 1217611205
98 );
99 }
100 }
101
102 // saving data
103 $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['data'][$entryIdentifier] = $data;
104
105 // tagging
106 foreach ($tags as $tag) {
107 if (!isset($GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['tags'][$tag])) {
108 $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['tags'][$tag] = array();
109 }
110
111 $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['tags'][$tag][] = $entryIdentifier;
112 }
113
114 }
115
116 /**
117 * Loads data from a cache file.
118 *
119 * @param string An identifier which describes the cache entry to load
120 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
121 * @author Ingo Renner <ingo@typo3.org>
122 */
123 public function load($entryIdentifier) {
124 $cacheEntry = FALSE;
125
126 if (isset($GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['data'][$entryIdentifier])) {
127 $cacheEntry = $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['data'][$entryIdentifier];
128 }
129
130 return $cacheEntry;
131 }
132
133 /**
134 * Checks if a cache entry with the specified identifier exists.
135 *
136 * @param unknown_type
137 * @return boolean TRUE if such an entry exists, FALSE if not
138 * @author Ingo Renner <ingo@typo3.org>
139 */
140 public function has($entryIdentifier) {
141 return isset($GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['data'][$entryIdentifier]);
142 }
143
144 /**
145 * Removes all cache entries matching the specified identifier.
146 * Usually this only affects one entry.
147 *
148 * @param string Specifies the cache entry to remove
149 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
150 * @author Ingo Renner <ingo@typo3.org>
151 */
152 public function remove($entryIdentifier) {
153 $cacheEntryFound = $this->has($entryIdentifier);
154
155 if ($cacheEntryFound) {
156 unset($GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['data'][$entryIdentifier]);
157 }
158
159 return $cacheEntryFound;
160 }
161
162 /**
163 * Finds and returns all cache entries which are tagged by the specified tag.
164 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
165 * the tag.
166 *
167 * @param string The tag to search for, the "*" wildcard is supported
168 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
169 * @author Ingo Renner <ingo@typo3.org>
170 */
171 public function findEntriesByTag($tag) {
172 $taggedEntries = array();
173
174 if (!empty($GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['tags'][$tag])) {
175 $taggedEntries = $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['tags'][$tag];
176 }
177
178 return $taggedEntries;
179 }
180
181 /**
182 * Finds and returns all cache entry identifiers which are tagged by the specified tags.
183 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
184 * a tag.
185 *
186 * @param array Array of tags to search for, the "*" wildcard is supported
187 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
188 * @author Ingo Renner <ingo@typo3.org>
189 */
190 public function findEntriesByTags(array $tags) {
191 $taggedEntries = array();
192 $foundEntries = array();
193
194 foreach ($tags as $tag) {
195 $taggedEntries[$tag] = $this->findEntriesByTag($tag);
196 }
197
198 $intersectedTaggedEntries = call_user_func_array('array_intersect', $taggedEntries);
199
200 foreach ($intersectedTaggedEntries as $entryIdentifier) {
201 $foundEntries[$entryIdentifier] = $entryIdentifier;
202 }
203
204 return $foundEntries;
205 }
206
207 /**
208 * Removes all cache entries of this cache.
209 *
210 * @return void
211 * @author Ingo Renner <ingo@typo3.org>
212 */
213 public function flush() {
214 $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['data'] = array();
215 $GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['tags'] = array();
216 }
217
218 /**
219 * Removes all cache entries of this cache which are tagged by the specified tag.
220 *
221 * @param string The tag the entries must have
222 * @return void
223 * @author Ingo Renner <ingo@typo3.org>
224 */
225 public function flushByTag($tag) {
226 unset($GLOBALS['typo3CacheStorage'][$this->cache->getIdentifier()]['tags'][$tag]);
227 }
228 }
229
230
231 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_globals.php']) {
232 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_globals.php']);
233 }
234
235 ?>