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