4d770f7bca22b2d0cc738439be7be2a09ed7d91c
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Backend / ApcBackend.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 APC.
19 *
20 * This backend uses the following types of 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 * - MD5 of path to TYPO3 and user running TYPO3
34 * This prefix makes sure that keys from the different installations do not
35 * conflict.
36 *
37 * @api
38 */
39 class ApcBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend implements \TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface
40 {
41 /**
42 * A prefix to separate stored data from other data possible stored in the APC
43 *
44 * @var string
45 */
46 protected $identifierPrefix;
47
48 /**
49 * Set the cache identifier prefix.
50 *
51 * @param string $identifierPrefix
52 */
53 protected function setIdentifierPrefix($identifierPrefix)
54 {
55 $this->identifierPrefix = $identifierPrefix;
56 }
57
58 /**
59 * Retrieves the cache identifier prefix.
60 *
61 * @return string
62 */
63 protected function getIdentifierPrefix()
64 {
65 return $this->identifierPrefix;
66 }
67
68 /**
69 * Constructs this backend
70 *
71 * @param string $context Unused, for backward compatibility only
72 * @param array $options Configuration options - unused here
73 * @throws \TYPO3\CMS\Core\Cache\Exception
74 */
75 public function __construct($context, array $options = [])
76 {
77 if (!extension_loaded('apc')) {
78 throw new \TYPO3\CMS\Core\Cache\Exception('The PHP extension "apc" or "apcu" must be installed and loaded in order to use the APC backend.', 1232985414);
79 }
80 if (PHP_SAPI === 'cli' && ini_get('apc.enable_cli') == 0) {
81 throw new \TYPO3\CMS\Core\Cache\Exception('The APC backend cannot be used because apc is disabled on CLI.', 1232985415);
82 }
83 parent::__construct($context, $options);
84 }
85
86 /**
87 * Initializes the identifier prefix when setting the cache.
88 *
89 * @param \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache
90 */
91 public function setCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache)
92 {
93 parent::setCache($cache);
94 $processUser = $this->getCurrentUserData();
95 $pathHash = \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5($this->getPathSite() . $processUser['name'] . $this->context . $cache->getIdentifier(), 12);
96 $this->setIdentifierPrefix('TYPO3_' . $pathHash);
97 }
98
99 /**
100 * Returns the current user data with posix_getpwuid or a default structure when
101 * posix_getpwuid is not available.
102 *
103 * @return array
104 */
105 protected function getCurrentUserData()
106 {
107 return extension_loaded('posix') ? posix_getpwuid(posix_geteuid()) : ['name' => 'default'];
108 }
109
110 /**
111 * Returns the PATH_site constant.
112 *
113 * @return string
114 */
115 protected function getPathSite()
116 {
117 return PATH_site;
118 }
119
120 /**
121 * Saves data in the cache.
122 *
123 * @param string $entryIdentifier An identifier for this specific cache entry
124 * @param string $data The data to be stored
125 * @param array $tags Tags to associate with this cache entry
126 * @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
127 * @throws \TYPO3\CMS\Core\Cache\Exception if no cache frontend has been set.
128 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException if $data is not a string
129 * @api
130 */
131 public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
132 {
133 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
134 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1232986818);
135 }
136 if (!is_string($data)) {
137 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1232986825);
138 }
139 $tags[] = '%APCBE%' . $this->cacheIdentifier;
140 $expiration = $lifetime ?? $this->defaultLifetime;
141 $success = apc_store($this->getIdentifierPrefix() . $entryIdentifier, $data, $expiration);
142 if ($success === true) {
143 $this->removeIdentifierFromAllTags($entryIdentifier);
144 $this->addIdentifierToTags($entryIdentifier, $tags);
145 } else {
146 $this->logger->alert('Error using APCu: Could not save data in the cache.');
147 }
148 }
149
150 /**
151 * Loads data from the cache.
152 *
153 * @param string $entryIdentifier An identifier which describes the cache entry to load
154 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
155 * @api
156 */
157 public function get($entryIdentifier)
158 {
159 $success = false;
160 $value = apc_fetch($this->getIdentifierPrefix() . $entryIdentifier, $success);
161 return $success ? $value : $success;
162 }
163
164 /**
165 * Checks if a cache entry with the specified identifier exists.
166 *
167 * @param string $entryIdentifier An identifier specifying the cache entry
168 * @return bool TRUE if such an entry exists, FALSE if not
169 * @api
170 */
171 public function has($entryIdentifier)
172 {
173 $success = false;
174 apc_fetch($this->getIdentifierPrefix() . $entryIdentifier, $success);
175 return $success;
176 }
177
178 /**
179 * Removes all cache entries matching the specified identifier.
180 * Usually this only affects one entry but if - for what reason ever -
181 * old entries for the identifier still exist, they are removed as well.
182 *
183 * @param string $entryIdentifier Specifies the cache entry to remove
184 * @return bool TRUE if (at least) an entry could be removed or FALSE if no entry was found
185 * @api
186 */
187 public function remove($entryIdentifier)
188 {
189 $this->removeIdentifierFromAllTags($entryIdentifier);
190 return apc_delete($this->getIdentifierPrefix() . $entryIdentifier);
191 }
192
193 /**
194 * Finds and returns all cache entry identifiers which are tagged by the
195 * specified tag.
196 *
197 * @param string $tag The tag to search for
198 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
199 * @api
200 */
201 public function findIdentifiersByTag($tag)
202 {
203 $success = false;
204 $identifiers = apc_fetch($this->getIdentifierPrefix() . 'tag_' . $tag, $success);
205 if ($success === false) {
206 return [];
207 }
208 return (array)$identifiers;
209 }
210
211 /**
212 * Finds all tags for the given identifier. This function uses reverse tag
213 * index to search for tags.
214 *
215 * @param string $identifier Identifier to find tags by
216 * @return array Array with tags
217 */
218 protected function findTagsByIdentifier($identifier)
219 {
220 $success = false;
221 $tags = apc_fetch($this->getIdentifierPrefix() . 'ident_' . $identifier, $success);
222 return $success ? (array)$tags : [];
223 }
224
225 /**
226 * Removes all cache entries of this cache.
227 *
228 * @throws \TYPO3\CMS\Core\Cache\Exception
229 * @api
230 */
231 public function flush()
232 {
233 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
234 throw new \TYPO3\CMS\Core\Cache\Exception('Yet no cache frontend has been set via setCache().', 1232986971);
235 }
236 $this->flushByTag('%APCBE%' . $this->cacheIdentifier);
237 }
238
239 /**
240 * Removes all cache entries of this cache which are tagged by the specified tag.
241 *
242 * @param string $tag The tag the entries must have
243 * @api
244 */
245 public function flushByTag($tag)
246 {
247 $identifiers = $this->findIdentifiersByTag($tag);
248 foreach ($identifiers as $identifier) {
249 $this->remove($identifier);
250 }
251 }
252
253 /**
254 * Associates the identifier with the given tags
255 *
256 * @param string $entryIdentifier
257 * @param array $tags
258 */
259 protected function addIdentifierToTags($entryIdentifier, array $tags)
260 {
261 // Get identifier-to-tag index to look for updates
262 $existingTags = $this->findTagsByIdentifier($entryIdentifier);
263 $existingTagsUpdated = false;
264
265 foreach ($tags as $tag) {
266 // Update tag-to-identifier index
267 $identifiers = $this->findIdentifiersByTag($tag);
268 if (!in_array($entryIdentifier, $identifiers, true)) {
269 $identifiers[] = $entryIdentifier;
270 apc_store($this->getIdentifierPrefix() . 'tag_' . $tag, $identifiers);
271 }
272 // Test if identifier-to-tag index needs update
273 if (!in_array($tag, $existingTags, true)) {
274 $existingTags[] = $tag;
275 $existingTagsUpdated = true;
276 }
277 }
278
279 // Update identifier-to-tag index if needed
280 if ($existingTagsUpdated) {
281 apc_store($this->getIdentifierPrefix() . 'ident_' . $entryIdentifier, $existingTags);
282 }
283 }
284
285 /**
286 * Removes association of the identifier with the given tags
287 *
288 * @param string $entryIdentifier
289 */
290 protected function removeIdentifierFromAllTags($entryIdentifier)
291 {
292 // Get tags for this identifier
293 $tags = $this->findTagsByIdentifier($entryIdentifier);
294 // Deassociate tags with this identifier
295 foreach ($tags as $tag) {
296 $identifiers = $this->findIdentifiersByTag($tag);
297 // Formally array_search() below should never return FALSE due to
298 // the behavior of findTagsByIdentifier(). But if reverse index is
299 // corrupted, we still can get 'FALSE' from array_search(). This is
300 // not a problem because we are removing this identifier from
301 // anywhere.
302 if (($key = array_search($entryIdentifier, $identifiers)) !== false) {
303 unset($identifiers[$key]);
304 if (!empty($identifiers)) {
305 apc_store($this->getIdentifierPrefix() . 'tag_' . $tag, $identifiers);
306 } else {
307 apc_delete($this->getIdentifierPrefix() . 'tag_' . $tag);
308 }
309 }
310 }
311 // Clear reverse tag index for this identifier
312 apc_delete($this->getIdentifierPrefix() . 'ident_' . $entryIdentifier);
313 }
314
315 /**
316 * Does nothing, as APC does GC itself
317 */
318 public function collectGarbage()
319 {
320 }
321 }