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