Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / t3lib / cache / frontend / class.t3lib_cache_frontend_abstractfrontend.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 * 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_frontend_AbstractFrontend implements t3lib_cache_frontend_Frontend {
36
37 /**
38 * @var string Identifies this cache
39 */
40 protected $identifier;
41
42 /**
43 * @var t3lib_cache_backend_AbstractBackend
44 */
45 protected $backend;
46
47 /**
48 * Constructs the cache
49 *
50 * @param string A identifier which describes this cache
51 * @param t3lib_cache_backend_Backend Backend to be used for this cache
52 * @author Robert Lemke <robert@typo3.org>
53 * @throws InvalidArgumentException if the identifier doesn't match PATTERN_ENTRYIDENTIFIER
54 * @internal
55 */
56 public function __construct($identifier, t3lib_cache_backend_Backend $backend) {
57 if (!preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier)) {
58 throw new InvalidArgumentException('"' . $identifier . '" is not a valid cache identifier.', 1203584729);
59 }
60
61 $this->identifier = $identifier;
62 $this->backend = $backend;
63 $this->backend->setCache($this);
64 }
65
66 /**
67 * Returns this cache's identifier
68 *
69 * @return string The identifier for this cache
70 * @author Robert Lemke <robert@typo3.org>
71 */
72 public function getIdentifier() {
73 return $this->identifier;
74 }
75
76 /**
77 * Returns the backend used by this cache
78 *
79 * @return t3lib_cache_backend_Backend The backend used by this cache
80 * @author Robert Lemke <robert@typo3.org>
81 */
82 public function getBackend() {
83 return $this->backend;
84 }
85
86 /**
87 * Checks if a cache entry with the specified identifier exists.
88 *
89 * @param string $entryIdentifier An identifier specifying the cache entry
90 * @return boolean TRUE if such an entry exists, FALSE if not
91 * @author Robert Lemke <robert@typo3.org>
92 * @author Karsten Dambekalns <karsten@typo3.org>
93 */
94 public function has($entryIdentifier) {
95 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
96 throw new InvalidArgumentException(
97 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
98 1233058486
99 );
100 }
101
102 return $this->backend->has($entryIdentifier);
103 }
104
105 /**
106 * Removes the given cache entry from the cache.
107 *
108 * @param string $entryIdentifier An identifier specifying the cache entry
109 * @return boolean TRUE if such an entry exists, FALSE if not
110 * @author Sebastian Kurfuerst <sebastian@typo3.org>
111 * @author Karsten Dambekalns <karsten@typo3.org>
112 */
113 public function remove($entryIdentifier) {
114 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
115 throw new InvalidArgumentException(
116 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
117 1233058495
118 );
119 }
120
121 return $this->backend->remove($entryIdentifier);
122 }
123
124 /**
125 * Removes all cache entries of this cache.
126 *
127 * @return void
128 * @author Robert Lemke <robert@typo3.org>
129 */
130 public function flush() {
131 $this->backend->flush();
132 }
133
134 /**
135 * Removes all cache entries of this cache which are tagged by the specified tag.
136 *
137 * @param string $tag The tag the entries must have
138 * @return void
139 * @author Robert Lemke <robert@typo3.org>
140 * @author Karsten Dambekalns <karsten@typo3.org>
141 */
142 public function flushByTag($tag) {
143 if (!$this->isValidTag($tag)) {
144 throw new InvalidArgumentException(
145 '"' . $tag . '" is not a valid tag for a cache entry.',
146 1233057359
147 );
148 }
149
150 $this->backend->flushByTag($tag);
151 }
152
153 /**
154 * Removes all cache entries of this cache which are tagged by the specified tag.
155 *
156 * @param array Array of tags to search for and to remove the cache entries, the "*" wildcard is supported
157 * @return void
158 * @author Ingo Renner <ingo@typo3.org>
159 */
160 public function flushByTags(array $tags) {
161 $this->backend->flushByTags($tags);
162 }
163
164 /**
165 * Does garbage collection
166 *
167 * @return void
168 * @author Karsten Dambekalns <karsten@typo3.org>
169 */
170 public function collectGarbage() {
171 $this->backend->collectGarbage();
172 }
173
174 /**
175 * Renders a tag which can be used to mark a cache entry as "depends on this class".
176 * Whenever the specified class is modified, all cache entries tagged with the
177 * class are flushed.
178 *
179 * If an empty string is specified as class name, the returned tag means
180 * "depends on any class".
181 *
182 * @param string The class name
183 * @return string Class Tag
184 * @author Robert Lemke <robert@typo3.org>
185 */
186 public function getClassTag($className = '') {
187 return ($className === '') ? self::TAG_CLASS : self::TAG_CLASS . str_replace('\\', '_', $className);
188 }
189
190 /**
191 * Checks the validity of an entry identifier. Returns true if it's valid.
192 *
193 * @param string An identifier to be checked for validity
194 * @return boolean
195 * @author Christian Jul Jensen <julle@typo3.org>
196 */
197 public function isValidEntryIdentifier($identifier) {
198 return preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier) === 1;
199 }
200
201 /**
202 * Checks the validity of a tag. Returns true if it's valid.
203 *
204 * @param string An identifier to be checked for validity
205 * @return boolean
206 * @author Robert Lemke <robert@typo3.org>
207 */
208 public function isValidTag($tag) {
209 return preg_match(self::PATTERN_TAG, $tag) === 1;
210 }
211
212 }
213
214
215 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php']) {
216 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php']);
217 }
218
219 ?>