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