[FEATURE] Adding hook in flushByTag
[Packages/TYPO3.CMS.git] / t3lib / cache / frontend / class.t3lib_cache_frontend_abstractfrontend.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 * An abstract cache
27 *
28 * This file is a backport from FLOW3
29 *
30 * @package TYPO3
31 * @subpackage t3lib_cache
32 * @author Robert Lemke <robert@typo3.org>
33 * @author Karsten Dambekalns <karsten@typo3.org>
34 * @api
35 * @scope prototype
36 */
37 abstract class t3lib_cache_frontend_AbstractFrontend implements t3lib_cache_frontend_Frontend {
38
39 /**
40 * Identifies this cache
41 * @var string
42 */
43 protected $identifier;
44
45 /**
46 * @var t3lib_cache_backend_AbstractBackend
47 */
48 protected $backend;
49
50 /**
51 * Constructs the cache
52 *
53 * @param string $identifier A identifier which describes this cache
54 * @param t3lib_cache_backend_Backend $backend Backend to be used for this cache
55 * @throws \InvalidArgumentException if the identifier doesn't match PATTERN_ENTRYIDENTIFIER
56 */
57 public function __construct($identifier, t3lib_cache_backend_Backend $backend) {
58 if (!preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier)) {
59 throw new \InvalidArgumentException('"' . $identifier . '" is not a valid cache identifier.', 1203584729);
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 * @api
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 * @api
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 * @throws \InvalidArgumentException If $entryIdentifier is invalid
92 * @api
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 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 * @api
110 */
111 public function remove($entryIdentifier) {
112 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
113 throw new \InvalidArgumentException(
114 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
115 1233058495
116 );
117 }
118 return $this->backend->remove($entryIdentifier);
119 }
120
121 /**
122 * Removes all cache entries of this cache.
123 *
124 * @return void
125 * @api
126 */
127 public function flush() {
128 $this->backend->flush();
129 }
130
131 /**
132 * Removes all cache entries of this cache which are tagged by the specified tag.
133 *
134 * @param string $tag The tag the entries must have
135 * @return void
136 * @api
137 */
138 public function flushByTag($tag) {
139 if (!$this->isValidTag($tag)) {
140 throw new \InvalidArgumentException(
141 '"' . $tag . '" is not a valid tag for a cache entry.',
142 1233057359
143 );
144 }
145
146 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php']['flushByTag'])) {
147 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php']['flushByTag'] as $_funcRef) {
148 $params = array('tag' => $tag);
149 t3lib_div::callUserFunction($_funcRef, $params, $this);
150 }
151 }
152
153 $this->backend->flushByTag($tag);
154 }
155
156 /**
157 * Removes all cache entries of this cache which are tagged by
158 * one of the specified tags.
159 * @param array $tags Array of tags to search for
160 * @return void
161 * @deprecated since 4.6, will be removed in 4.8
162 * @api
163 */
164 public function flushByTags(array $tags) {
165 t3lib_div::deprecationLog('flushByTags is deprecated since 4.6 and will be removed in 4.8');
166 foreach ($tags as $tag) {
167 $this->backend->flushByTag($tag);
168 }
169 }
170
171 /**
172 * Does garbage collection
173 *
174 * @return void
175 * @api
176 */
177 public function collectGarbage() {
178 $this->backend->collectGarbage();
179 }
180
181 /**
182 * Renders a tag which can be used to mark a cache entry as "depends on this class".
183 * Whenever the specified class is modified, all cache entries tagged with the
184 * class are flushed.
185 *
186 * If an empty string is specified as class name, the returned tag means "depends on any class".
187 *
188 * @param string $className The class name
189 * @return string Class Tag
190 * @api
191 * @deprecated since TYPO3 4.6 - Use t3lib_cache_Manager::getClassTag() instead
192 */
193 public function getClassTag($className = '') {
194 return t3lib_cache_Manager::getClassTag($className);
195 }
196
197 /**
198 * Checks the validity of an entry identifier. Returns TRUE if it's valid.
199 *
200 * @param string $identifier An identifier to be checked for validity
201 * @return boolean
202 * @api
203 */
204 public function isValidEntryIdentifier($identifier) {
205 return preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier) === 1;
206 }
207
208 /**
209 * Checks the validity of a tag. Returns TRUE if it's valid.
210 *
211 * @param string $tag An identifier to be checked for validity
212 * @return boolean
213 * @api
214 */
215 public function isValidTag($tag) {
216 return preg_match(self::PATTERN_TAG, $tag) === 1;
217 }
218 }
219 ?>