‪TYPO3CMS  10.4
WincacheBackend.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
43 {
49  protected ‪$identifierPrefix;
50 
58  public function ‪__construct(‪$context, array $options = [])
59  {
60  if (!extension_loaded('wincache')) {
61  throw new ‪Exception('The PHP extension "wincache" must be installed and loaded in order to use the wincache backend.', 1343331520);
62  }
63  parent::__construct(‪$context, $options);
64  }
65 
77  public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
78  {
79  if (!$this->cache instanceof ‪FrontendInterface) {
80  throw new ‪Exception('No cache frontend has been set yet via setCache().', 1343331521);
81  }
82  if (!is_string($data)) {
83  throw new ‪InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1343331522);
84  }
85  $tags[] = '%WCBE%' . $this->cache->getIdentifier();
86  $expiration = $lifetime ?? ‪$this->defaultLifetime;
87  $success = wincache_ucache_set($this->identifierPrefix . $entryIdentifier, $data, $expiration);
88  if ($success === true) {
89  $this->‪removeIdentifierFromAllTags($entryIdentifier);
90  $this->‪addIdentifierToTags($entryIdentifier, $tags);
91  } else {
92  throw new ‪Exception('Could not set value.', 1343331523);
93  }
94  }
95 
102  public function get($entryIdentifier)
103  {
104  $success = false;
105  $value = wincache_ucache_get($this->identifierPrefix . $entryIdentifier, $success);
106  return $success ? $value : $success;
107  }
108 
115  public function ‪has($entryIdentifier)
116  {
117  return wincache_ucache_exists($this->identifierPrefix . $entryIdentifier);
118  }
119 
128  public function remove($entryIdentifier)
129  {
130  $this->‪removeIdentifierFromAllTags($entryIdentifier);
131  return wincache_ucache_delete($this->identifierPrefix . $entryIdentifier);
132  }
133 
141  public function ‪findIdentifiersByTag($tag)
142  {
143  $success = false;
144  $identifiers = wincache_ucache_get($this->identifierPrefix . 'tag_' . $tag, $success);
145  if ($success === false) {
146  return [];
147  }
148  return (array)$identifiers;
149  }
150 
158  protected function ‪findTagsByIdentifier($identifier)
159  {
160  $success = false;
161  $tags = wincache_ucache_get($this->identifierPrefix . 'ident_' . $identifier, $success);
162  return $success ? (array)$tags : [];
163  }
164 
170  public function ‪flush()
171  {
172  if (!$this->cache instanceof ‪FrontendInterface) {
173  throw new ‪Exception('Yet no cache frontend has been set via setCache().', 1343331524);
174  }
175  $this->‪flushByTag('%WCBE%' . $this->cache->getIdentifier());
176  }
177 
184  public function ‪flushByTag($tag)
185  {
186  $identifiers = $this->‪findIdentifiersByTag($tag);
187  foreach ($identifiers as $identifier) {
188  $this->remove($identifier);
189  }
190  }
191 
198  protected function ‪addIdentifierToTags($entryIdentifier, array $tags)
199  {
200  // Get identifier-to-tag index to look for updates
201  $existingTags = $this->‪findTagsByIdentifier($entryIdentifier);
202  $existingTagsUpdated = false;
203 
204  foreach ($tags as $tag) {
205  // Update tag-to-identifier index
206  $identifiers = $this->‪findIdentifiersByTag($tag);
207  if (!in_array($entryIdentifier, $identifiers, true)) {
208  $identifiers[] = $entryIdentifier;
209  wincache_ucache_set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
210  }
211  // Test if identifier-to-tag index needs update
212  if (!in_array($tag, $existingTags, true)) {
213  $existingTags[] = $tag;
214  $existingTagsUpdated = true;
215  }
216  }
217 
218  // Update identifier-to-tag index if needed
219  if ($existingTagsUpdated) {
220  wincache_ucache_set($this->identifierPrefix . 'ident_' . $entryIdentifier, $existingTags);
221  }
222  }
223 
229  protected function ‪removeIdentifierFromAllTags($entryIdentifier)
230  {
231  // Get tags for this identifier
232  $tags = $this->‪findTagsByIdentifier($entryIdentifier);
233  // Deassociate tags with this identifier
234  foreach ($tags as $tag) {
235  $identifiers = $this->‪findIdentifiersByTag($tag);
236  // Formally array_search() below should never return false due to
237  // the behavior of findTagsByIdentifier(). But if reverse index is
238  // corrupted, we still can get 'false' from array_search(). This is
239  // not a problem because we are removing this identifier from
240  // anywhere.
241  if (($key = array_search($entryIdentifier, $identifiers)) !== false) {
242  unset($identifiers[$key]);
243  if (!empty($identifiers)) {
244  wincache_ucache_set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
245  } else {
246  wincache_ucache_delete($this->identifierPrefix . 'tag_' . $tag);
247  }
248  }
249  }
250  // Clear reverse tag index for this identifier
251  wincache_ucache_delete($this->identifierPrefix . 'ident_' . $entryIdentifier);
252  }
253 
257  public function ‪collectGarbage()
258  {
259  }
260 }
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\flush
‪flush()
Definition: WincacheBackend.php:169
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\flushByTag
‪flushByTag($tag)
Definition: WincacheBackend.php:183
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\findTagsByIdentifier
‪array findTagsByIdentifier($identifier)
Definition: WincacheBackend.php:157
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend
Definition: WincacheBackend.php:43
‪TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface
Definition: TaggableBackendInterface.php:22
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\$identifierPrefix
‪string $identifierPrefix
Definition: WincacheBackend.php:48
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$defaultLifetime
‪int $defaultLifetime
Definition: AbstractBackend.php:56
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\findIdentifiersByTag
‪array findIdentifiersByTag($tag)
Definition: WincacheBackend.php:140
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\collectGarbage
‪collectGarbage()
Definition: WincacheBackend.php:256
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:24
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\removeIdentifierFromAllTags
‪removeIdentifierFromAllTags($entryIdentifier)
Definition: WincacheBackend.php:228
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\has
‪bool has($entryIdentifier)
Definition: WincacheBackend.php:114
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend
Definition: AbstractBackend.php:28
‪TYPO3\CMS\Core\Cache\Backend
Definition: AbstractBackend.php:16
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$context
‪string $context
Definition: AbstractBackend.php:50
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\__construct
‪__construct($context, array $options=[])
Definition: WincacheBackend.php:57
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\addIdentifierToTags
‪addIdentifierToTags($entryIdentifier, array $tags)
Definition: WincacheBackend.php:197
‪TYPO3\CMS\Core\Cache\Exception
Definition: Exception.php:22