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