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