‪TYPO3CMS  9.5
WincacheBackend.php
Go to the documentation of this file.
1 <?php
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 
20 
42 {
48  protected ‪$identifierPrefix;
49 
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 
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 
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 
114  public function ‪has($entryIdentifier)
115  {
116  return wincache_ucache_exists($this->identifierPrefix . $entryIdentifier);
117  }
118 
127  public function remove($entryIdentifier)
128  {
129  $this->‪removeIdentifierFromAllTags($entryIdentifier);
130  return wincache_ucache_delete($this->identifierPrefix . $entryIdentifier);
131  }
132 
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 
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 
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 
183  public function ‪flushByTag($tag)
184  {
185  $identifiers = $this->‪findIdentifiersByTag($tag);
186  foreach ($identifiers as $identifier) {
187  $this->remove($identifier);
188  }
189  }
190 
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 
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 
256  public function ‪collectGarbage()
257  {
258  }
259 }
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\flush
‪flush()
Definition: WincacheBackend.php:168
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\flushByTag
‪flushByTag($tag)
Definition: WincacheBackend.php:182
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\findTagsByIdentifier
‪array findTagsByIdentifier($identifier)
Definition: WincacheBackend.php:156
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend
Definition: WincacheBackend.php:42
‪TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface
Definition: TaggableBackendInterface.php:21
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\$identifierPrefix
‪string $identifierPrefix
Definition: WincacheBackend.php:47
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$defaultLifetime
‪int $defaultLifetime
Definition: AbstractBackend.php:54
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\findIdentifiersByTag
‪array findIdentifiersByTag($tag)
Definition: WincacheBackend.php:139
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\collectGarbage
‪collectGarbage()
Definition: WincacheBackend.php:255
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:2
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:21
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\removeIdentifierFromAllTags
‪removeIdentifierFromAllTags($entryIdentifier)
Definition: WincacheBackend.php:227
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\has
‪bool has($entryIdentifier)
Definition: WincacheBackend.php:113
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend
Definition: AbstractBackend.php:26
‪TYPO3\CMS\Core\Cache\Backend
Definition: AbstractBackend.php:2
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$context
‪string $context
Definition: AbstractBackend.php:48
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\__construct
‪__construct($context, array $options=[])
Definition: WincacheBackend.php:56
‪TYPO3\CMS\Core\Cache\Backend\WincacheBackend\addIdentifierToTags
‪addIdentifierToTags($entryIdentifier, array $tags)
Definition: WincacheBackend.php:196
‪TYPO3\CMS\Core\Cache\Exception
Definition: Exception.php:21