Fixed bug #18016
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_transientmemorybackend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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 stores cache entries during one script run.
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @api
34 */
35 class t3lib_cache_backend_TransientMemoryBackend extends t3lib_cache_backend_AbstractBackend {
36
37 /**
38 * @var array
39 */
40 protected $entries = array();
41
42 /**
43 * @var array
44 */
45 protected $tagsAndEntries = array();
46
47 /**
48 * Saves data in the cache.
49 *
50 * @param string $entryIdentifier An identifier for this specific cache entry
51 * @param string $data The data to be stored
52 * @param array $tags Tags to associate with this cache entry
53 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
54 * @return void
55 * @throws t3lib_cache_Exception if no cache frontend has been set.
56 * @author Robert Lemke <robert@typo3.org>
57 */
58 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
59 if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
60 throw new t3lib_cache_Exception('No cache frontend has been set yet via setCache().', 1238244992);
61 }
62 if (!is_string($data)) {
63 throw new t3lib_cache_exception_InvalidData('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1238244993);
64 }
65 $this->entries[$entryIdentifier] = $data;
66 foreach ($tags as $tag) {
67 $this->tagsAndEntries[$tag][$entryIdentifier] = TRUE;
68 }
69 }
70
71 /**
72 * Loads data from the cache.
73 *
74 * @param string $entryIdentifier An identifier which describes the cache entry to load
75 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
76 * @author Robert Lemke <robert@typo3.org>
77 */
78 public function get($entryIdentifier) {
79 return (isset($this->entries[$entryIdentifier])) ? $this->entries[$entryIdentifier] : FALSE;
80 }
81
82 /**
83 * Checks if a cache entry with the specified identifier exists.
84 *
85 * @param string $entryIdentifier An identifier specifying the cache entry
86 * @return boolean TRUE if such an entry exists, FALSE if not
87 * @author Robert Lemke <robert@typo3.org>
88 */
89 public function has($entryIdentifier) {
90 return isset($this->entries[$entryIdentifier]);
91 }
92
93 /**
94 * Removes all cache entries matching the specified identifier.
95 *
96 * @param string $entryIdentifier Specifies the cache entry to remove
97 * @return boolean TRUE if the entry could be removed or FALSE if no entry was found
98 * @author Robert Lemke <robert@typo3.org>
99 */
100 public function remove($entryIdentifier) {
101 if (isset($this->entries[$entryIdentifier])) {
102 unset($this->entries[$entryIdentifier]);
103 foreach (array_keys($this->tagsAndEntries) as $tag) {
104 if (isset($this->tagsAndEntries[$tag][$entryIdentifier])) {
105 unset ($this->tagsAndEntries[$tag][$entryIdentifier]);
106 }
107 }
108 return TRUE;
109 } else {
110 return FALSE;
111 }
112 }
113
114 /**
115 * Finds and returns all cache entry identifiers which are tagged by the
116 * specified tag.
117 *
118 * @param string $tag The tag to search for
119 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
120 * @author Robert Lemke <robert@typo3.org>
121 */
122 public function findIdentifiersByTag($tag) {
123 if (isset($this->tagsAndEntries[$tag])) {
124 return array_keys($this->tagsAndEntries[$tag]);
125 } else {
126 return array();
127 }
128 }
129
130 /**
131 * Finds and returns all cache entry identifiers which are tagged by the
132 * specified tags.
133 * The asterisk ("*") is allowed as a wildcard at the beginning and the end
134 * of a tag.
135 *
136 * @param array Array of tags to search for, the "*" wildcard is supported
137 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
138 * @author Ingo Renner <ingo@typo3.org>
139 */
140 public function findIdentifiersByTags(array $tags) {
141 $taggedEntries = array();
142 $foundEntries = array();
143
144 foreach ($tags as $tag) {
145 $taggedEntries[$tag] = $this->findIdentifiersByTag($tag);
146 }
147
148 $intersectedTaggedEntries = call_user_func_array('array_intersect', $taggedEntries);
149
150 foreach ($intersectedTaggedEntries as $entryIdentifier) {
151 $foundEntries[$entryIdentifier] = $entryIdentifier;
152 }
153
154 return $foundEntries;
155 }
156
157 /**
158 * Removes all cache entries of this cache.
159 *
160 * @return void
161 * @author Robert Lemke <robert@typo3.org>
162 */
163 public function flush() {
164 $this->entries = array();
165 $this->tagsAndEntries = array();
166 }
167
168 /**
169 * Removes all cache entries of this cache which are tagged by the specified tag.
170 *
171 * @param string $tag The tag the entries must have
172 * @return void
173 * @author Robert Lemke <robert@typo3.org>
174 */
175 public function flushByTag($tag) {
176 $identifiers = $this->findIdentifiersByTag($tag);
177 foreach ($identifiers as $identifier) {
178 $this->remove($identifier);
179 }
180 }
181
182 /**
183 * Removes all cache entries of this cache which are tagged by the specified tags.
184 *
185 * @param array The tags the entries must have
186 * @return void
187 * @author Ingo Renner <ingo@typo3.org>
188 */
189 public function flushByTags(array $tags) {
190 foreach ($tags as $tag) {
191 $this->flushByTag($tag);
192 }
193 }
194
195 /**
196 * Does nothing
197 *
198 * @return void
199 * @author Robert Lemke <robert@typo3.org>
200 */
201 public function collectGarbage() {
202 }
203 }
204
205
206 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_transientmemorybackend.php'])) {
207 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_transientmemorybackend.php']);
208 }
209
210 ?>