[BUGFIX] Memcached backend identifierPrefix only based on PATH_site
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Backend / MemcachedBackend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Backend;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * A caching backend which stores cache entries by using Memcached.
18 *
19 * This backend uses the following types of Memcache keys:
20 * - tag_xxx
21 * xxx is tag name, value is array of associated identifiers identifier. This
22 * is "forward" tag index. It is mainly used for obtaining content by tag
23 * (get identifier by tag -> get content by identifier)
24 * - ident_xxx
25 * xxx is identifier, value is array of associated tags. This is "reverse" tag
26 * index. It provides quick access for all tags associated with this identifier
27 * and used when removing the identifier
28 *
29 * Each key is prepended with a prefix. By default prefix consists from two parts
30 * separated by underscore character and ends in yet another underscore character:
31 * - "TYPO3"
32 * - Current site path obtained from the PATH_site constant
33 * This prefix makes sure that keys from the different installations do not
34 * conflict.
35 *
36 * Note: When using the Memcached backend to store values of more than ~1 MB,
37 * the data will be split into chunks to make them fit into the memcached limits.
38 *
39 * This file is a backport from FLOW3 by Ingo Renner.
40 *
41 * @author Robert Lemke <robert@typo3.org>
42 * @author Christian Jul Jensen <julle@typo3.org>
43 * @author Karsten Dambekalns <karsten@typo3.org>
44 * @author Dmitry Dulepov <dmitry@typo3.org>
45 * @api
46 */
47 class MemcachedBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend implements \TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface {
48
49 /**
50 * Max bucket size, (1024*1024)-42 bytes
51 *
52 * @var int
53 */
54 const MAX_BUCKET_SIZE = 1048534;
55 /**
56 * Instance of the PHP Memcache class
57 *
58 * @var \Memcache
59 */
60 protected $memcache;
61
62 /**
63 * Array of Memcache server configurations
64 *
65 * @var array
66 */
67 protected $servers = array();
68
69 /**
70 * Indicates whether the memcache uses compression or not (requires zlib),
71 * either 0 or MEMCACHE_COMPRESSED
72 *
73 * @var int
74 */
75 protected $flags;
76
77 /**
78 * A prefix to separate stored data from other data possibly stored in the memcache
79 *
80 * @var string
81 */
82 protected $identifierPrefix;
83
84 /**
85 * Constructs this backend
86 *
87 * @param string $context FLOW3's application context
88 * @param array $options Configuration options - depends on the actual backend
89 * @throws \TYPO3\CMS\Core\Cache\Exception if memcache is not installed
90 */
91 public function __construct($context, array $options = array()) {
92 if (!extension_loaded('memcache')) {
93 throw new \TYPO3\CMS\Core\Cache\Exception('The PHP extension "memcache" must be installed and loaded in ' . 'order to use the Memcached backend.', 1213987706);
94 }
95 parent::__construct($context, $options);
96 }
97
98 /**
99 * Setter for servers to be used. Expects an array, the values are expected
100 * to be formatted like "<host>[:<port>]" or "unix://<path>"
101 *
102 * @param array $servers An array of servers to add.
103 * @return void
104 * @api
105 */
106 protected function setServers(array $servers) {
107 $this->servers = $servers;
108 }
109
110 /**
111 * Setter for compression flags bit
112 *
113 * @param boolean $useCompression
114 * @return void
115 * @api
116 */
117 protected function setCompression($useCompression) {
118 if ($useCompression === TRUE) {
119 $this->flags ^= MEMCACHE_COMPRESSED;
120 } else {
121 $this->flags &= ~MEMCACHE_COMPRESSED;
122 }
123 }
124
125 /**
126 * Initializes the identifier prefix
127 *
128 * @return void
129 * @throws \TYPO3\CMS\Core\Cache\Exception
130 */
131 public function initializeObject() {
132 if (!count($this->servers)) {
133 throw new \TYPO3\CMS\Core\Cache\Exception('No servers were given to Memcache', 1213115903);
134 }
135 $this->memcache = new \Memcache();
136 $defaultPort = ini_get('memcache.default_port');
137 foreach ($this->servers as $server) {
138 if (substr($server, 0, 7) == 'unix://') {
139 $host = $server;
140 $port = 0;
141 } else {
142 if (substr($server, 0, 6) === 'tcp://') {
143 $server = substr($server, 6);
144 }
145 if (strpos($server, ':') !== FALSE) {
146 list($host, $port) = explode(':', $server, 2);
147 } else {
148 $host = $server;
149 $port = $defaultPort;
150 }
151 }
152 $this->memcache->addserver($host, $port);
153 }
154 }
155
156 /**
157 * Initializes the identifier prefix when setting the cache.
158 *
159 * @param \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache The frontend for this backend
160 * @return void
161 */
162 public function setCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache) {
163 parent::setCache($cache);
164 $identifierHash = substr(md5(PATH_site . $this->context . $this->cacheIdentifier), 0, 12);
165 $this->identifierPrefix = 'TYPO3_' . $identifierHash . '_';
166 }
167
168 /**
169 * Saves data in the cache.
170 *
171 * @param string $entryIdentifier An identifier for this specific cache entry
172 * @param string $data The data to be stored
173 * @param array $tags Tags to associate with this cache entry
174 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
175 * @return void
176 * @throws \TYPO3\CMS\Core\Cache\Exception if no cache frontend has been set.
177 * @throws \InvalidArgumentException if the identifier is not valid or the final memcached key is longer than 250 characters
178 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException if $data is not a string
179 * @api
180 */
181 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
182 if (strlen($this->identifierPrefix . $entryIdentifier) > 250) {
183 throw new \InvalidArgumentException('Could not set value. Key more than 250 characters (' . $this->identifierPrefix . $entryIdentifier . ').', 1232969508);
184 }
185 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
186 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1207149215);
187 }
188 if (!is_string($data)) {
189 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1207149231);
190 }
191 $tags[] = '%MEMCACHEBE%' . $this->cacheIdentifier;
192 $expiration = $lifetime !== NULL ? $lifetime : $this->defaultLifetime;
193 // Memcached consideres values over 2592000 sec (30 days) as UNIX timestamp
194 // thus $expiration should be converted from lifetime to UNIX timestamp
195 if ($expiration > 2592000) {
196 $expiration += $GLOBALS['EXEC_TIME'];
197 }
198 try {
199 if (strlen($data) > self::MAX_BUCKET_SIZE) {
200 $data = str_split($data, 1024 * 1000);
201 $success = TRUE;
202 $chunkNumber = 1;
203 foreach ($data as $chunk) {
204 $success = $success && $this->memcache->set($this->identifierPrefix . $entryIdentifier . '_chunk_' . $chunkNumber, $chunk, $this->flags, $expiration);
205 $chunkNumber++;
206 }
207 $success = $success && $this->memcache->set($this->identifierPrefix . $entryIdentifier, 'TYPO3*chunked:' . $chunkNumber, $this->flags, $expiration);
208 } else {
209 $success = $this->memcache->set($this->identifierPrefix . $entryIdentifier, $data, $this->flags, $expiration);
210 }
211 if ($success === TRUE) {
212 $this->removeIdentifierFromAllTags($entryIdentifier);
213 $this->addIdentifierToTags($entryIdentifier, $tags);
214 } else {
215 throw new \TYPO3\CMS\Core\Cache\Exception('Could not set data to memcache server.', 1275830266);
216 }
217 } catch (\Exception $exception) {
218 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Memcache: could not set value. Reason: ' . $exception->getMessage(), 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_WARNING);
219 }
220 }
221
222 /**
223 * Loads data from the cache.
224 *
225 * @param string $entryIdentifier An identifier which describes the cache entry to load
226 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
227 * @api
228 */
229 public function get($entryIdentifier) {
230 $value = $this->memcache->get($this->identifierPrefix . $entryIdentifier);
231 if (substr($value, 0, 14) === 'TYPO3*chunked:') {
232 list(, $chunkCount) = explode(':', $value);
233 $value = '';
234 for ($chunkNumber = 1; $chunkNumber < $chunkCount; $chunkNumber++) {
235 $value .= $this->memcache->get($this->identifierPrefix . $entryIdentifier . '_chunk_' . $chunkNumber);
236 }
237 }
238 return $value;
239 }
240
241 /**
242 * Checks if a cache entry with the specified identifier exists.
243 *
244 * @param string $entryIdentifier An identifier specifying the cache entry
245 * @return boolean TRUE if such an entry exists, FALSE if not
246 * @api
247 */
248 public function has($entryIdentifier) {
249 return $this->memcache->get($this->identifierPrefix . $entryIdentifier) !== FALSE;
250 }
251
252 /**
253 * Removes all cache entries matching the specified identifier.
254 * Usually this only affects one entry but if - for what reason ever -
255 * old entries for the identifier still exist, they are removed as well.
256 *
257 * @param string $entryIdentifier Specifies the cache entry to remove
258 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
259 * @api
260 */
261 public function remove($entryIdentifier) {
262 $this->removeIdentifierFromAllTags($entryIdentifier);
263 return $this->memcache->delete($this->identifierPrefix . $entryIdentifier, 0);
264 }
265
266 /**
267 * Finds and returns all cache entry identifiers which are tagged by the
268 * specified tag.
269 *
270 * @param string $tag The tag to search for
271 * @return array An array of entries with all matching entries. An empty array if no entries matched
272 * @api
273 */
274 public function findIdentifiersByTag($tag) {
275 $identifiers = $this->memcache->get($this->identifierPrefix . 'tag_' . $tag);
276 if ($identifiers !== FALSE) {
277 return (array) $identifiers;
278 } else {
279 return array();
280 }
281 }
282
283 /**
284 * Removes all cache entries of this cache.
285 *
286 * @return void
287 * @throws \TYPO3\CMS\Core\Cache\Exception
288 * @api
289 */
290 public function flush() {
291 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
292 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set via setCache() yet.', 1204111376);
293 }
294 $this->flushByTag('%MEMCACHEBE%' . $this->cacheIdentifier);
295 }
296
297 /**
298 * Removes all cache entries of this cache which are tagged by the specified tag.
299 *
300 * @param string $tag The tag the entries must have
301 * @return void
302 * @api
303 */
304 public function flushByTag($tag) {
305 $identifiers = $this->findIdentifiersByTag($tag);
306 foreach ($identifiers as $identifier) {
307 $this->remove($identifier);
308 }
309 }
310
311 /**
312 * Associates the identifier with the given tags
313 *
314 * @param string $entryIdentifier
315 * @param array $tags
316 * @return void
317 */
318 protected function addIdentifierToTags($entryIdentifier, array $tags) {
319 // Get identifier-to-tag index to look for updates
320 $existingTags = $this->findTagsByIdentifier($entryIdentifier);
321 $existingTagsUpdated = FALSE;
322
323 foreach ($tags as $tag) {
324 // Update tag-to-identifier index
325 $identifiers = $this->findIdentifiersByTag($tag);
326 if (!in_array($entryIdentifier, $identifiers, TRUE)) {
327 $identifiers[] = $entryIdentifier;
328 $this->memcache->set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
329 }
330 // Test if identifier-to-tag index needs update
331 if (!in_array($tag, $existingTags, TRUE)) {
332 $existingTags[] = $tag;
333 $existingTagsUpdated = TRUE;
334 }
335 }
336
337 // Update identifier-to-tag index if needed
338 if ($existingTagsUpdated) {
339 $this->memcache->set($this->identifierPrefix . 'ident_' . $entryIdentifier, $existingTags);
340 }
341 }
342
343 /**
344 * Removes association of the identifier with the given tags
345 *
346 * @param string $entryIdentifier
347 * @param array Array of tags
348 * @return void
349 */
350 protected function removeIdentifierFromAllTags($entryIdentifier) {
351 // Get tags for this identifier
352 $tags = $this->findTagsByIdentifier($entryIdentifier);
353 // Deassociate tags with this identifier
354 foreach ($tags as $tag) {
355 $identifiers = $this->findIdentifiersByTag($tag);
356 // Formally array_search() below should never return FALSE due to
357 // the behavior of findTagsByIdentifier(). But if reverse index is
358 // corrupted, we still can get 'FALSE' from array_search(). This is
359 // not a problem because we are removing this identifier from
360 // anywhere.
361 if (($key = array_search($entryIdentifier, $identifiers)) !== FALSE) {
362 unset($identifiers[$key]);
363 if (count($identifiers)) {
364 $this->memcache->set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
365 } else {
366 $this->memcache->delete($this->identifierPrefix . 'tag_' . $tag, 0);
367 }
368 }
369 }
370 // Clear reverse tag index for this identifier
371 $this->memcache->delete($this->identifierPrefix . 'ident_' . $entryIdentifier, 0);
372 }
373
374 /**
375 * Finds all tags for the given identifier. This function uses reverse tag
376 * index to search for tags.
377 *
378 * @param string $identifier Identifier to find tags by
379 * @return array
380 * @api
381 */
382 protected function findTagsByIdentifier($identifier) {
383 $tags = $this->memcache->get($this->identifierPrefix . 'ident_' . $identifier);
384 return $tags === FALSE ? array() : (array) $tags;
385 }
386
387 /**
388 * Does nothing, as memcached does GC itself
389 *
390 * @return void
391 * @api
392 */
393 public function collectGarbage() {
394
395 }
396
397 }