Fixed an inconsitency with between the name, the documentation and the functionality...
[Packages/TYPO3.CMS.git] / t3lib / cache / class.t3lib_cache_abstractcache.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 * An abstract cache
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @version $Id$
34 */
35 abstract class t3lib_cache_AbstractCache {
36
37 const PATTERN_IDENTIFIER = '/^[a-zA-Z0-9_%]{1,250}$/';
38
39 /**
40 * @var string Identifies this cache
41 */
42 protected $identifier;
43
44 /**
45 * @var t3lib_cache_AbstractBackend
46 */
47 protected $backend;
48
49 /**
50 * Constructs the cache
51 *
52 * @param string A identifier which describes this cache
53 * @param t3lib_cache_AbstractBackend Backend to be used for this cache
54 * @author Robert Lemke <robert@typo3.org>
55 * @throws InvalidArgumentException if the identifier doesn't match PATTERN_IDENTIFIER
56 */
57 public function __construct($identifier, t3lib_cache_AbstractBackend $backend) {
58 if (!preg_match(self::PATTERN_IDENTIFIER, $identifier)) {
59 throw new InvalidArgumentException('"' . $identifier . '" is not a valid cache identifier.', 1203584729);
60 }
61
62 $this->identifier = $identifier;
63 $this->backend = $backend;
64 $this->backend->setCache($this);
65 }
66
67 /**
68 * Returns this cache's identifier
69 *
70 * @return string The identifier for this cache
71 * @author Robert Lemke <robert@typo3.org>
72 */
73 public function getIdentifier() {
74 return $this->identifier;
75 }
76
77 /**
78 * Returns the backend used by this cache
79 *
80 * @return t3lib_cache_AbstractBackend The backend used by this cache
81 * @author Robert Lemke <robert@typo3.org>
82 */
83 public function getBackend() {
84 return $this->backend;
85 }
86
87 /**
88 * Saves data in the cache.
89 *
90 * @param string Something which identifies the data - depends on concrete cache
91 * @param mixed The data to cache - also depends on the concrete cache implementation
92 * @param array Tags to associate with this cache entry
93 * @return void
94 * @author Robert Lemke <robert@typo3.org>
95 */
96 abstract public function save($entryIdentifier, $data, array $tags = array(), $lifetime = null);
97
98 /**
99 * Loads data from the cache.
100 *
101 * @param string Something which identifies the cache entry - depends on concrete cache
102 * @return mixed
103 * @author Robert Lemke <robert@typo3.org>
104 */
105 abstract public function load($entryIdentifier);
106
107 /**
108 * Finds, loads, and returns all cache entries which are tagged by the specified tag.
109 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
110 * the tag.
111 *
112 * @param string The tag to search for, the "*" wildcard is supported
113 * @return array An array with all matching entries. An empty array if no entries matched
114 * @author Ingo Renner <ingo@typo3.org>
115 */
116 public function loadByTag($tag) {
117 $loadedEntries = array();
118 $foundEntries = $this->findEntriesByTag($tag);
119
120 foreach($foundEntries as $foundEntryIdentifier) {
121 $loadedEntries[$foundEntryIdentifier] = $this->load($foundEntryIdentifier);
122 }
123
124 return $loadedEntries;
125 }
126
127 /**
128 * Finds and returns all cache entry identifiers which are tagged by the specified tags.
129 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
130 * a tag.
131 *
132 * @param string The tag 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 */
135 public function findEntriesByTag($tag) {
136 return $this->backend->findEntriesByTag($tag);
137 }
138
139 /**
140 * Finds and returns all cache entry identifiers which are tagged by the specified tags.
141 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
142 * a tag.
143 *
144 * @param array Array of tags to search for, the "*" wildcard is supported
145 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
146 */
147 public function findEntriesByTags(array $tags) {
148 return $this->backend->findEntriesByTags($tags);
149 }
150
151 /**
152 * Checks if a cache entry with the specified identifier exists.
153 *
154 * @param string An identifier specifying the cache entry
155 * @return boolean TRUE if such an entry exists, FALSE if not
156 * @author Robert Lemke <robert@typo3.org>
157 */
158 abstract public function has($entryIdentifier);
159
160 /**
161 * Removes the given cache entry from the cache.
162 *
163 * @param string An identifier specifying the cache entry
164 * @return boolean TRUE if such an entry exists, FALSE if not
165 * @author Sebastian Kurfuerst <sebastian@typo3.org>
166 */
167 abstract public function remove($entryIdentifier);
168
169 /**
170 * Removes all cache entries of this cache.
171 *
172 * @return void
173 * @author Robert Lemke <robert@typo3.org>
174 */
175 public function flush() {
176 $this->backend->flush();
177 }
178
179 /**
180 * Removes all cache entries of this cache which are tagged by the specified tag.
181 *
182 * @param string $tag The tag the entries must have
183 * @return void
184 * @author Robert Lemke <robert@typo3.org>
185 */
186 public function flushByTag($tag) {
187 $this->backend->flushByTag($tag);
188 }
189
190 /**
191 * Removes all cache entries of this cache which are tagged by the specified tag.
192 *
193 * @param array Array of tags to search for and to remove the cache entries, the "*" wildcard is supported
194 * @return void
195 * @author Ingo Renner <ingo@typo3.org>
196 */
197 public function flushByTags(array $tags) {
198 $this->backend->flushByTags($tags);
199 }
200 }
201
202
203 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_abstractcache.php']) {
204 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_abstractcache.php']);
205 }
206
207 ?>