TYPO3 CMS  TYPO3_6-2
WincacheBackend.php
Go to the documentation of this file.
1 <?php
3 
39 
45  protected $identifierPrefix;
46 
54  public function __construct($context, array $options = array()) {
55  if (!extension_loaded('wincache')) {
56  throw new \TYPO3\CMS\Core\Cache\Exception('The PHP extension "wincache" must be installed and loaded in order to use the wincache backend.', 1343331520);
57  }
58  parent::__construct($context, $options);
59  }
60 
73  public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
74  if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
75  throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1343331521);
76  }
77  if (!is_string($data)) {
78  throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1343331522);
79  }
80  $tags[] = '%WCBE%' . $this->cache->getIdentifier();
81  $expiration = $lifetime !== NULL ? $lifetime : $this->defaultLifetime;
82  $success = wincache_ucache_set($this->identifierPrefix . $entryIdentifier, $data, $expiration);
83  if ($success === TRUE) {
84  $this->removeIdentifierFromAllTags($entryIdentifier);
85  $this->addIdentifierToTags($entryIdentifier, $tags);
86  } else {
87  throw new \TYPO3\CMS\Core\Cache\Exception('Could not set value.', 1343331523);
88  }
89  }
90 
97  public function get($entryIdentifier) {
98  $success = FALSE;
99  $value = wincache_ucache_get($this->identifierPrefix . $entryIdentifier, $success);
100  return $success ? $value : $success;
101  }
102 
109  public function has($entryIdentifier) {
110  return wincache_ucache_exists($this->identifierPrefix . $entryIdentifier);
111  }
112 
121  public function remove($entryIdentifier) {
122  $this->removeIdentifierFromAllTags($entryIdentifier);
123  return wincache_ucache_delete($this->identifierPrefix . $entryIdentifier);
124  }
125 
133  public function findIdentifiersByTag($tag) {
134  $success = FALSE;
135  $identifiers = wincache_ucache_get($this->identifierPrefix . 'tag_' . $tag, $success);
136  if ($success === FALSE) {
137  return array();
138  } else {
139  return (array) $identifiers;
140  }
141  }
142 
150  protected function findTagsByIdentifier($identifier) {
151  $success = FALSE;
152  $tags = wincache_ucache_get($this->identifierPrefix . 'ident_' . $identifier, $success);
153  return $success ? (array) $tags : array();
154  }
155 
162  public function flush() {
163  if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
164  throw new \TYPO3\CMS\Core\Cache\Exception('Yet no cache frontend has been set via setCache().', 1343331524);
165  }
166  $this->flushByTag('%WCBE%' . $this->cache->getIdentifier());
167  }
168 
176  public function flushByTag($tag) {
177  $identifiers = $this->findIdentifiersByTag($tag);
178  foreach ($identifiers as $identifier) {
179  $this->remove($identifier);
180  }
181  }
182 
190  protected function addIdentifierToTags($entryIdentifier, array $tags) {
191  // Get identifier-to-tag index to look for updates
192  $existingTags = $this->findTagsByIdentifier($entryIdentifier);
193  $existingTagsUpdated = FALSE;
194 
195  foreach ($tags as $tag) {
196  // Update tag-to-identifier index
197  $identifiers = $this->findIdentifiersByTag($tag);
198  if (!in_array($entryIdentifier, $identifiers, TRUE)) {
199  $identifiers[] = $entryIdentifier;
200  wincache_ucache_set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
201  }
202  // Test if identifier-to-tag index needs update
203  if (!in_array($tag, $existingTags, TRUE)) {
204  $existingTags[] = $tag;
205  $existingTagsUpdated = TRUE;
206  }
207  }
208 
209  // Update identifier-to-tag index if needed
210  if ($existingTagsUpdated) {
211  wincache_ucache_set($this->identifierPrefix . 'ident_' . $entryIdentifier, $existingTags);
212  }
213  }
214 
221  protected function removeIdentifierFromAllTags($entryIdentifier) {
222  // Get tags for this identifier
223  $tags = $this->findTagsByIdentifier($entryIdentifier);
224  // Deassociate tags with this identifier
225  foreach ($tags as $tag) {
226  $identifiers = $this->findIdentifiersByTag($tag);
227  // Formally array_search() below should never return false due to
228  // the behavior of findTagsByIdentifier(). But if reverse index is
229  // corrupted, we still can get 'false' from array_search(). This is
230  // not a problem because we are removing this identifier from
231  // anywhere.
232  if (($key = array_search($entryIdentifier, $identifiers)) !== FALSE) {
233  unset($identifiers[$key]);
234  if (count($identifiers)) {
235  wincache_ucache_set($this->identifierPrefix . 'tag_' . $tag, $identifiers);
236  } else {
237  wincache_ucache_delete($this->identifierPrefix . 'tag_' . $tag);
238  }
239  }
240  }
241  // Clear reverse tag index for this identifier
242  wincache_ucache_delete($this->identifierPrefix . 'ident_' . $entryIdentifier);
243  }
244 
250  public function collectGarbage() {
251 
252  }
253 
254 }
addIdentifierToTags($entryIdentifier, array $tags)
__construct($context, array $options=array())