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