[TASK] Streamline imports in PHP cache classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Backend / WincacheBackend.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\Cache\Exception;
18 use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
19 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
20
21 /**
22 * A caching backend which stores cache entries by using wincache.
23 *
24 * This backend uses the following types of keys:
25 * - tag_xxx
26 * xxx is tag name, value is array of associated identifiers identifier. This
27 * is "forward" tag index. It is mainly used for obtaining content by tag
28 * (get identifier by tag -> get content by identifier)
29 * - ident_xxx
30 * xxx is identifier, value is array of associated tags. This is "reverse" tag
31 * index. It provides quick access for all tags associated with this identifier
32 * and used when removing the identifier
33 *
34 * Each key is prepended with a prefix. By default prefix consists from two parts
35 * separated by underscore character and ends in yet another underscore character:
36 * - "TYPO3"
37 * - MD5 of script path and filename and SAPI name
38 * This prefix makes sure that keys from the different installations do not
39 * conflict.
40 */
41 class WincacheBackend extends AbstractBackend implements TaggableBackendInterface
42 {
43 /**
44 * A prefix to separate stored data from other data possible stored in the wincache
45 *
46 * @var string
47 */
48 protected $identifierPrefix;
49
50 /**
51 * Constructs this backend
52 *
53 * @param string $context Unused, for backward compatibility only
54 * @param array $options Configuration options
55 * @throws Exception If wincache PHP extension is not loaded
56 */
57 public function __construct($context, array $options = [])
58 {
59 if (!extension_loaded('wincache')) {
60 throw new Exception('The PHP extension "wincache" must be installed and loaded in order to use the wincache backend.', 1343331520);
61 }
62 parent::__construct($context, $options);
63 }
64
65 /**
66 * Saves data in the cache
67 *
68 * @param string $entryIdentifier An identifier for this specific cache entry
69 * @param string $data The data to be stored
70 * @param array $tags Tags to associate with this cache entry
71 * @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
72 * @throws Exception if no cache frontend has been set
73 * @throws \InvalidArgumentException if the identifier is not valid
74 * @throws InvalidDataException if $data is not a string
75 */
76 public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
77 {
78 if (!$this->cache instanceof FrontendInterface) {
79 throw new Exception('No cache frontend has been set yet via setCache().', 1343331521);
80 }
81 if (!is_string($data)) {
82 throw new InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1343331522);
83 }
84 $tags[] = '%WCBE%' . $this->cache->getIdentifier();
85 $expiration = $lifetime ?? $this->defaultLifetime;
86 $success = wincache_ucache_set($this->identifierPrefix . $entryIdentifier, $data, $expiration);
87 if ($success === true) {
88 $this->removeIdentifierFromAllTags($entryIdentifier);
89 $this->addIdentifierToTags($entryIdentifier, $tags);
90 } else {
91 throw new Exception('Could not set value.', 1343331523);
92 }
93 }
94
95 /**
96 * Loads data from the cache
97 *
98 * @param string $entryIdentifier An identifier which describes the cache entry to load
99 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
100 */
101 public function get($entryIdentifier)
102 {
103 $success = false;
104 $value = wincache_ucache_get($this->identifierPrefix . $entryIdentifier, $success);
105 return $success ? $value : $success;
106 }
107
108 /**
109 * Checks if a cache entry with the specified identifier exists
110 *
111 * @param string $entryIdentifier An identifier specifying the cache entry
112 * @return bool TRUE if such an entry exists, FALSE if not
113 */
114 public function has($entryIdentifier)
115 {
116 return wincache_ucache_exists($this->identifierPrefix . $entryIdentifier);
117 }
118
119 /**
120 * Removes all cache entries matching the specified identifier.
121 * Usually this only affects one entry but if - for what reason ever -
122 * old entries for the identifier still exist, they are removed as well.
123 *
124 * @param string $entryIdentifier Specifies the cache entry to remove
125 * @return bool TRUE if (at least) an entry could be removed or FALSE if no entry was found
126 */
127 public function remove($entryIdentifier)
128 {
129 $this->removeIdentifierFromAllTags($entryIdentifier);
130 return wincache_ucache_delete($this->identifierPrefix . $entryIdentifier);
131 }
132
133 /**
134 * Finds and returns all cache entry identifiers which are tagged by the
135 * specified tag.
136 *
137 * @param string $tag The tag to search for
138 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
139 */
140 public function findIdentifiersByTag($tag)
141 {
142 $success = false;
143 $identifiers = wincache_ucache_get($this->identifierPrefix . 'tag_' . $tag, $success);
144 if ($success === false) {
145 return [];
146 }
147 return (array)$identifiers;
148 }
149
150 /**
151 * Finds all tags for the given identifier. This function uses reverse tag
152 * index to search for tags.
153 *
154 * @param string $identifier Identifier to find tags by
155 * @return array Array with tags
156 */
157 protected function findTagsByIdentifier($identifier)
158 {
159 $success = false;
160 $tags = wincache_ucache_get($this->identifierPrefix . 'ident_' . $identifier, $success);
161 return $success ? (array)$tags : [];
162 }
163
164 /**
165 * Removes all cache entries of this cache
166 *
167 * @throws Exception
168 */
169 public function flush()
170 {
171 if (!$this->cache instanceof FrontendInterface) {
172 throw new Exception('Yet no cache frontend has been set via setCache().', 1343331524);
173 }
174 $this->flushByTag('%WCBE%' . $this->cache->getIdentifier());
175 }
176
177 /**
178 * Removes all cache entries of this cache which are tagged by the specified
179 * tag.
180 *
181 * @param string $tag The tag the entries must have
182 */
183 public function flushByTag($tag)
184 {
185 $identifiers = $this->findIdentifiersByTag($tag);
186 foreach ($identifiers as $identifier) {
187 $this->remove($identifier);
188 }
189 }
190
191 /**
192 * Associates the identifier with the given tags
193 *
194 * @param string $entryIdentifier
195 * @param array $tags
196 */
197 protected function addIdentifierToTags($entryIdentifier, array $tags)
198 {
199 // Get identifier-to-tag index to look for updates
200 $existingTags = $this->findTagsByIdentifier($entryIdentifier);
201 $existingTagsUpdated = false;
202
203 foreach ($tags as $tag) {
204 // Update tag-to-identifier index
205 $identifiers = $this->findIdentifiersByTag($tag);
206 if (!in_array($entryIdentifier, $identifiers, true)) {
207 $identifiers[] = $entryIdentifier;
208 wincache_ucache_set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
209 }
210 // Test if identifier-to-tag index needs update
211 if (!in_array($tag, $existingTags, true)) {
212 $existingTags[] = $tag;
213 $existingTagsUpdated = true;
214 }
215 }
216
217 // Update identifier-to-tag index if needed
218 if ($existingTagsUpdated) {
219 wincache_ucache_set($this->identifierPrefix . 'ident_' . $entryIdentifier, $existingTags);
220 }
221 }
222
223 /**
224 * Removes association of the identifier with the given tags
225 *
226 * @param string $entryIdentifier
227 */
228 protected function removeIdentifierFromAllTags($entryIdentifier)
229 {
230 // Get tags for this identifier
231 $tags = $this->findTagsByIdentifier($entryIdentifier);
232 // Deassociate tags with this identifier
233 foreach ($tags as $tag) {
234 $identifiers = $this->findIdentifiersByTag($tag);
235 // Formally array_search() below should never return false due to
236 // the behavior of findTagsByIdentifier(). But if reverse index is
237 // corrupted, we still can get 'false' from array_search(). This is
238 // not a problem because we are removing this identifier from
239 // anywhere.
240 if (($key = array_search($entryIdentifier, $identifiers)) !== false) {
241 unset($identifiers[$key]);
242 if (!empty($identifiers)) {
243 wincache_ucache_set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
244 } else {
245 wincache_ucache_delete($this->identifierPrefix . 'tag_' . $tag);
246 }
247 }
248 }
249 // Clear reverse tag index for this identifier
250 wincache_ucache_delete($this->identifierPrefix . 'ident_' . $entryIdentifier);
251 }
252
253 /**
254 * Does nothing, as wincache does GC itself
255 */
256 public function collectGarbage()
257 {
258 }
259 }