Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_transientmemorybackend.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 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 * @version $Id$
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) throw new t3lib_cache_Exception('No cache frontend has been set yet via setCache().', 1238244992);
60 if (!is_string($data)) throw new t3lib_cache_exception_InvalidData('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1238244993);
61 $this->entries[$entryIdentifier] = $data;
62 foreach ($tags as $tag) {
63 $this->tagsAndEntries[$tag][$entryIdentifier] = TRUE;
64 }
65 }
66
67 /**
68 * Loads data from the cache.
69 *
70 * @param string $entryIdentifier An identifier which describes the cache entry to load
71 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
72 * @author Robert Lemke <robert@typo3.org>
73 */
74 public function get($entryIdentifier) {
75 return (isset($this->entries[$entryIdentifier])) ? $this->entries[$entryIdentifier] : FALSE;
76 }
77
78 /**
79 * Checks if a cache entry with the specified identifier exists.
80 *
81 * @param string $entryIdentifier An identifier specifying the cache entry
82 * @return boolean TRUE if such an entry exists, FALSE if not
83 * @author Robert Lemke <robert@typo3.org>
84 */
85 public function has($entryIdentifier) {
86 return isset($this->entries[$entryIdentifier]);
87 }
88
89 /**
90 * Removes all cache entries matching the specified identifier.
91 *
92 * @param string $entryIdentifier Specifies the cache entry to remove
93 * @return boolean TRUE if the entry could be removed or FALSE if no entry was found
94 * @author Robert Lemke <robert@typo3.org>
95 */
96 public function remove($entryIdentifier) {
97 if (isset($this->entries[$entryIdentifier])) {
98 unset($this->entries[$entryIdentifier]);
99 foreach (array_keys($this->tagsAndEntries) as $tag) {
100 if (isset($this->tagsAndEntries[$tag][$entryIdentifier])) {
101 unset ($this->tagsAndEntries[$tag][$entryIdentifier]);
102 }
103 }
104 return TRUE;
105 } else {
106 return FALSE;
107 }
108 }
109
110 /**
111 * Finds and returns all cache entry identifiers which are tagged by the
112 * specified tag.
113 *
114 * @param string $tag The tag to search for
115 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
116 * @author Robert Lemke <robert@typo3.org>
117 */
118 public function findIdentifiersByTag($tag) {
119 if (isset($this->tagsAndEntries[$tag])) {
120 return array_keys($this->tagsAndEntries[$tag]);
121 } else {
122 return array();
123 }
124 }
125
126 /**
127 * Finds and returns all cache entry identifiers which are tagged by the
128 * specified tags.
129 * The asterisk ("*") is allowed as a wildcard at the beginning and the end
130 * of a tag.
131 *
132 * @param array Array of tags to search for, the "*" wildcard is supported
133 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
134 * @author Ingo Renner <ingo@typo3.org>
135 */
136 public function findIdentifiersByTags(array $tags) {
137 $taggedEntries = array();
138 $foundEntries = array();
139
140 foreach ($tags as $tag) {
141 $taggedEntries[$tag] = $this->findIdentifiersByTag($tag);
142 }
143
144 $intersectedTaggedEntries = call_user_func_array('array_intersect', $taggedEntries);
145
146 foreach ($intersectedTaggedEntries as $entryIdentifier) {
147 $foundEntries[$entryIdentifier] = $entryIdentifier;
148 }
149
150 return $foundEntries;
151 }
152
153 /**
154 * Removes all cache entries of this cache.
155 *
156 * @return void
157 * @author Robert Lemke <robert@typo3.org>
158 */
159 public function flush() {
160 $this->entries = array();
161 $this->tagsAndEntries = array();
162 }
163
164 /**
165 * Removes all cache entries of this cache which are tagged by the specified tag.
166 *
167 * @param string $tag The tag the entries must have
168 * @return void
169 * @author Robert Lemke <robert@typo3.org>
170 */
171 public function flushByTag($tag) {
172 $identifiers = $this->findIdentifiersByTag($tag);
173 foreach ($identifiers as $identifier) {
174 $this->remove($identifier);
175 }
176 }
177
178 /**
179 * Removes all cache entries of this cache which are tagged by the specified tags.
180 *
181 * @param array The tags the entries must have
182 * @return void
183 * @author Ingo Renner <ingo@typo3.org>
184 */
185 public function flushByTags(array $tags) {
186 foreach ($tags as $tag) {
187 $this->flushByTag($tag);
188 }
189 }
190
191 /**
192 * Does nothing
193 *
194 * @return void
195 * @author Robert Lemke <robert@typo3.org>
196 */
197 public function collectGarbage() {
198 }
199 }
200
201
202 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_transientmemorybackend.php']) {
203 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_transientmemorybackend.php']);
204 }
205
206 ?>