[TASK] Re-work/simplify copyright header in PHP files - Part 2
[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 $this->identifierPrefix = 'TYPO3_' . md5(PATH_site) . '_';
165 }
166
167 /**
168 * Saves data in the cache.
169 *
170 * @param string $entryIdentifier An identifier for this specific cache entry
171 * @param string $data The data to be stored
172 * @param array $tags Tags to associate with this cache entry
173 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
174 * @return void
175 * @throws \TYPO3\CMS\Core\Cache\Exception if no cache frontend has been set.
176 * @throws \InvalidArgumentException if the identifier is not valid or the final memcached key is longer than 250 characters
177 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException if $data is not a string
178 * @api
179 */
180 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
181 if (strlen($this->identifierPrefix . $entryIdentifier) > 250) {
182 throw new \InvalidArgumentException('Could not set value. Key more than 250 characters (' . $this->identifierPrefix . $entryIdentifier . ').', 1232969508);
183 }
184 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
185 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1207149215);
186 }
187 if (!is_string($data)) {
188 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1207149231);
189 }
190 $tags[] = '%MEMCACHEBE%' . $this->cacheIdentifier;
191 $expiration = $lifetime !== NULL ? $lifetime : $this->defaultLifetime;
192 // Memcached consideres values over 2592000 sec (30 days) as UNIX timestamp
193 // thus $expiration should be converted from lifetime to UNIX timestamp
194 if ($expiration > 2592000) {
195 $expiration += $GLOBALS['EXEC_TIME'];
196 }
197 try {
198 if (strlen($data) > self::MAX_BUCKET_SIZE) {
199 $data = str_split($data, 1024 * 1000);
200 $success = TRUE;
201 $chunkNumber = 1;
202 foreach ($data as $chunk) {
203 $success = $success && $this->memcache->set($this->identifierPrefix . $entryIdentifier . '_chunk_' . $chunkNumber, $chunk, $this->flags, $expiration);
204 $chunkNumber++;
205 }
206 $success = $success && $this->memcache->set($this->identifierPrefix . $entryIdentifier, 'TYPO3*chunked:' . $chunkNumber, $this->flags, $expiration);
207 } else {
208 $success = $this->memcache->set($this->identifierPrefix . $entryIdentifier, $data, $this->flags, $expiration);
209 }
210 if ($success === TRUE) {
211 $this->removeIdentifierFromAllTags($entryIdentifier);
212 $this->addIdentifierToTags($entryIdentifier, $tags);
213 } else {
214 throw new \TYPO3\CMS\Core\Cache\Exception('Could not set data to memcache server.', 1275830266);
215 }
216 } catch (\Exception $exception) {
217 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Memcache: could not set value. Reason: ' . $exception->getMessage(), 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_WARNING);
218 }
219 }
220
221 /**
222 * Loads data from the cache.
223 *
224 * @param string $entryIdentifier An identifier which describes the cache entry to load
225 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
226 * @api
227 */
228 public function get($entryIdentifier) {
229 $value = $this->memcache->get($this->identifierPrefix . $entryIdentifier);
230 if (substr($value, 0, 14) === 'TYPO3*chunked:') {
231 list(, $chunkCount) = explode(':', $value);
232 $value = '';
233 for ($chunkNumber = 1; $chunkNumber < $chunkCount; $chunkNumber++) {
234 $value .= $this->memcache->get($this->identifierPrefix . $entryIdentifier . '_chunk_' . $chunkNumber);
235 }
236 }
237 return $value;
238 }
239
240 /**
241 * Checks if a cache entry with the specified identifier exists.
242 *
243 * @param string $entryIdentifier An identifier specifying the cache entry
244 * @return boolean TRUE if such an entry exists, FALSE if not
245 * @api
246 */
247 public function has($entryIdentifier) {
248 return $this->memcache->get($this->identifierPrefix . $entryIdentifier) !== FALSE;
249 }
250
251 /**
252 * Removes all cache entries matching the specified identifier.
253 * Usually this only affects one entry but if - for what reason ever -
254 * old entries for the identifier still exist, they are removed as well.
255 *
256 * @param string $entryIdentifier Specifies the cache entry to remove
257 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
258 * @api
259 */
260 public function remove($entryIdentifier) {
261 $this->removeIdentifierFromAllTags($entryIdentifier);
262 return $this->memcache->delete($this->identifierPrefix . $entryIdentifier, 0);
263 }
264
265 /**
266 * Finds and returns all cache entry identifiers which are tagged by the
267 * specified tag.
268 *
269 * @param string $tag The tag to search for
270 * @return array An array of entries with all matching entries. An empty array if no entries matched
271 * @api
272 */
273 public function findIdentifiersByTag($tag) {
274 $identifiers = $this->memcache->get($this->identifierPrefix . 'tag_' . $tag);
275 if ($identifiers !== FALSE) {
276 return (array) $identifiers;
277 } else {
278 return array();
279 }
280 }
281
282 /**
283 * Removes all cache entries of this cache.
284 *
285 * @return void
286 * @throws \TYPO3\CMS\Core\Cache\Exception
287 * @api
288 */
289 public function flush() {
290 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
291 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set via setCache() yet.', 1204111376);
292 }
293 $this->flushByTag('%MEMCACHEBE%' . $this->cacheIdentifier);
294 }
295
296 /**
297 * Removes all cache entries of this cache which are tagged by the specified tag.
298 *
299 * @param string $tag The tag the entries must have
300 * @return void
301 * @api
302 */
303 public function flushByTag($tag) {
304 $identifiers = $this->findIdentifiersByTag($tag);
305 foreach ($identifiers as $identifier) {
306 $this->remove($identifier);
307 }
308 }
309
310 /**
311 * Associates the identifier with the given tags
312 *
313 * @param string $entryIdentifier
314 * @param array $tags
315 * @return void
316 */
317 protected function addIdentifierToTags($entryIdentifier, array $tags) {
318 foreach ($tags as $tag) {
319 // Update tag-to-identifier index
320 $identifiers = $this->findIdentifiersByTag($tag);
321 if (array_search($entryIdentifier, $identifiers) === FALSE) {
322 $identifiers[] = $entryIdentifier;
323 $this->memcache->set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
324 }
325 // Update identifier-to-tag index
326 $existingTags = $this->findTagsByIdentifier($entryIdentifier);
327 if (array_search($tag, $existingTags) === FALSE) {
328 $this->memcache->set($this->identifierPrefix . 'ident_' . $entryIdentifier, array_merge($existingTags, $tags));
329 }
330 }
331 }
332
333 /**
334 * Removes association of the identifier with the given tags
335 *
336 * @param string $entryIdentifier
337 * @param array Array of tags
338 * @return void
339 */
340 protected function removeIdentifierFromAllTags($entryIdentifier) {
341 // Get tags for this identifier
342 $tags = $this->findTagsByIdentifier($entryIdentifier);
343 // Deassociate tags with this identifier
344 foreach ($tags as $tag) {
345 $identifiers = $this->findIdentifiersByTag($tag);
346 // Formally array_search() below should never return FALSE due to
347 // the behavior of findTagsByIdentifier(). But if reverse index is
348 // corrupted, we still can get 'FALSE' from array_search(). This is
349 // not a problem because we are removing this identifier from
350 // anywhere.
351 if (($key = array_search($entryIdentifier, $identifiers)) !== FALSE) {
352 unset($identifiers[$key]);
353 if (count($identifiers)) {
354 $this->memcache->set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
355 } else {
356 $this->memcache->delete($this->identifierPrefix . 'tag_' . $tag, 0);
357 }
358 }
359 }
360 // Clear reverse tag index for this identifier
361 $this->memcache->delete($this->identifierPrefix . 'ident_' . $entryIdentifier, 0);
362 }
363
364 /**
365 * Finds all tags for the given identifier. This function uses reverse tag
366 * index to search for tags.
367 *
368 * @param string $identifier Identifier to find tags by
369 * @return array
370 * @api
371 */
372 protected function findTagsByIdentifier($identifier) {
373 $tags = $this->memcache->get($this->identifierPrefix . 'ident_' . $identifier);
374 return $tags === FALSE ? array() : (array) $tags;
375 }
376
377 /**
378 * Does nothing, as memcached does GC itself
379 *
380 * @return void
381 * @api
382 */
383 public function collectGarbage() {
384
385 }
386
387 }