‪TYPO3CMS  ‪main
FileBackend.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 
25 
30 {
31  public const ‪SEPARATOR = '^';
32  public const ‪EXPIRYTIME_FORMAT = 'YmdHis';
33  public const ‪EXPIRYTIME_LENGTH = 14;
34  public const ‪DATASIZE_DIGITS = 10;
40  protected ‪$cacheEntryFileExtension = '';
41 
45  protected ‪$cacheEntryIdentifiers = [];
46 
50  protected ‪$frozen = false;
51 
64  public function ‪freeze()
65  {
66  if ($this->frozen === true) {
67  throw new \RuntimeException(sprintf('The cache "%s" is already frozen.', $this->cacheIdentifier), 1323353176);
68  }
69  $cacheEntryFileExtensionLength = strlen($this->cacheEntryFileExtension);
70  for ($directoryIterator = new \DirectoryIterator($this->cacheDirectory); $directoryIterator->valid(); $directoryIterator->next()) {
71  if ($directoryIterator->isDot()) {
72  continue;
73  }
74  if ($cacheEntryFileExtensionLength > 0) {
75  $entryIdentifier = substr($directoryIterator->getFilename(), 0, -$cacheEntryFileExtensionLength);
76  } else {
77  $entryIdentifier = $directoryIterator->getFilename();
78  }
79  $this->cacheEntryIdentifiers[$entryIdentifier] = true;
80  file_put_contents($this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension, $this->get($entryIdentifier));
81  }
82  file_put_contents($this->cacheDirectory . 'FrozenCache.data', serialize($this->cacheEntryIdentifiers));
83  $this->frozen = true;
84  }
85 
91  public function ‪isFrozen()
92  {
93  return ‪$this->frozen;
94  }
95 
106  {
107  parent::setCache(‪$cache);
108  if (file_exists($this->cacheDirectory . 'FrozenCache.data')) {
109  $this->frozen = true;
110  $this->cacheEntryIdentifiers = unserialize((string)file_get_contents($this->cacheDirectory . 'FrozenCache.data'));
111  }
112  }
113 
126  public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
127  {
128  if (!is_string($data)) {
129  throw new InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1204481674);
130  }
131  if ($entryIdentifier !== ‪PathUtility::basename($entryIdentifier)) {
132  throw new \InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1282073032);
133  }
134  if ($entryIdentifier === '') {
135  throw new \InvalidArgumentException('The specified entry identifier must not be empty.', 1298114280);
136  }
137  if ($this->frozen === true) {
138  throw new \RuntimeException(sprintf('Cannot add or modify cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1323344192);
139  }
140  $this->remove($entryIdentifier);
141  $temporaryCacheEntryPathAndFilename = $this->cacheDirectory . ‪StringUtility::getUniqueId() . '.temp';
142  $lifetime = (int)($lifetime ?? $this->defaultLifetime);
143  $expiryTime = $lifetime === 0 ? 0 : (int)(‪$GLOBALS['EXEC_TIME'] + $lifetime);
144  $metaData = str_pad((string)$expiryTime, self::EXPIRYTIME_LENGTH) . implode(' ', $tags) . str_pad((string)strlen($data), self::DATASIZE_DIGITS);
145  $result = file_put_contents($temporaryCacheEntryPathAndFilename, $data . $metaData);
146  ‪GeneralUtility::fixPermissions($temporaryCacheEntryPathAndFilename);
147  if ($result === false) {
148  throw new Exception('The temporary cache file "' . $temporaryCacheEntryPathAndFilename . '" could not be written.', 1204026251);
149  }
150  $i = 0;
151  $cacheEntryPathAndFilename = $this->cacheDirectory . $entryIdentifier . ‪$this->cacheEntryFileExtension;
152  while (($result = rename($temporaryCacheEntryPathAndFilename, $cacheEntryPathAndFilename)) === false && $i < 5) {
153  $i++;
154  }
155  if ($result === false) {
156  throw new Exception('The cache file "' . $cacheEntryPathAndFilename . '" could not be written.', 1222361632);
157  }
158  if ($this->cacheEntryFileExtension === '.php') {
159  GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($cacheEntryPathAndFilename);
160  }
161  }
162 
170  public function get($entryIdentifier)
171  {
172  if ($this->frozen === true) {
173  return isset($this->cacheEntryIdentifiers[$entryIdentifier]) ? file_get_contents($this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension) : false;
174  }
175  if ($entryIdentifier !== ‪PathUtility::basename($entryIdentifier)) {
176  throw new \InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1282073033);
177  }
178  $pathAndFilename = $this->cacheDirectory . $entryIdentifier . ‪$this->cacheEntryFileExtension;
179  if ($this->‪isCacheFileExpired($pathAndFilename)) {
180  return false;
181  }
182  $dataSize = (int)file_get_contents(
183  $pathAndFilename,
184  false,
185  null,
186  filesize($pathAndFilename) - ‪self::DATASIZE_DIGITS,
188  );
189  return file_get_contents($pathAndFilename, false, null, 0, $dataSize);
190  }
191 
199  public function ‪has($entryIdentifier)
200  {
201  if ($this->frozen === true) {
202  return isset($this->cacheEntryIdentifiers[$entryIdentifier]);
203  }
204  if ($entryIdentifier !== ‪PathUtility::basename($entryIdentifier)) {
205  throw new \InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1282073034);
206  }
207  return !$this->‪isCacheFileExpired($this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension);
208  }
209 
219  public function remove($entryIdentifier)
220  {
221  if ($this->frozen === true) {
222  throw new \RuntimeException(sprintf('Cannot remove cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1323344193);
223  }
224  return parent::remove($entryIdentifier);
225  }
226 
234  public function ‪findIdentifiersByTag($searchedTag)
235  {
236  $entryIdentifiers = [];
237  $now = ‪$GLOBALS['EXEC_TIME'];
238  $cacheEntryFileExtensionLength = strlen($this->cacheEntryFileExtension);
239  for ($directoryIterator = GeneralUtility::makeInstance(\DirectoryIterator::class, $this->cacheDirectory); $directoryIterator->valid(); $directoryIterator->next()) {
240  if ($directoryIterator->isDot()) {
241  continue;
242  }
243  $cacheEntryPathAndFilename = $directoryIterator->getPathname();
244  $index = (int)file_get_contents(
245  $cacheEntryPathAndFilename,
246  false,
247  null,
248  filesize($cacheEntryPathAndFilename) - ‪self::DATASIZE_DIGITS,
250  );
251  $metaData = (string)file_get_contents($cacheEntryPathAndFilename, false, null, $index);
252  $expiryTime = (int)substr($metaData, 0, self::EXPIRYTIME_LENGTH);
253  if ($expiryTime !== 0 && $expiryTime < $now) {
254  continue;
255  }
256  if (in_array($searchedTag, explode(' ', substr($metaData, self::EXPIRYTIME_LENGTH, -self::DATASIZE_DIGITS)))) {
257  if ($cacheEntryFileExtensionLength > 0) {
258  $entryIdentifiers[] = substr($directoryIterator->getFilename(), 0, -$cacheEntryFileExtensionLength);
259  } else {
260  $entryIdentifiers[] = $directoryIterator->getFilename();
261  }
262  }
263  }
264  return $entryIdentifiers;
265  }
266 
270  public function ‪flush()
271  {
272  parent::flush();
273  if ($this->frozen === true) {
274  $this->frozen = false;
275  }
276  }
277 
283  public function ‪flushByTag($tag)
284  {
285  $identifiers = $this->‪findIdentifiersByTag($tag);
286  if (empty($identifiers)) {
287  return;
288  }
289  foreach ($identifiers as $entryIdentifier) {
290  $this->remove($entryIdentifier);
291  }
292  }
293 
301  protected function ‪isCacheFileExpired($cacheEntryPathAndFilename)
302  {
303  if (file_exists($cacheEntryPathAndFilename) === false) {
304  return true;
305  }
306  $index = (int)file_get_contents(
307  $cacheEntryPathAndFilename,
308  false,
309  null,
310  filesize($cacheEntryPathAndFilename) - ‪self::DATASIZE_DIGITS,
312  );
313  $expiryTime = (int)file_get_contents($cacheEntryPathAndFilename, false, null, $index, self::EXPIRYTIME_LENGTH);
314  return $expiryTime !== 0 && $expiryTime < ‪$GLOBALS['EXEC_TIME'];
315  }
316 
320  public function ‪collectGarbage()
321  {
322  if ($this->frozen === true) {
323  return;
324  }
325  for ($directoryIterator = new \DirectoryIterator($this->cacheDirectory); $directoryIterator->valid(); $directoryIterator->next()) {
326  if ($directoryIterator->isDot()) {
327  continue;
328  }
329  if ($this->‪isCacheFileExpired($directoryIterator->getPathname())) {
330  $cacheEntryFileExtensionLength = strlen($this->cacheEntryFileExtension);
331  if ($cacheEntryFileExtensionLength > 0) {
332  $this->remove(substr($directoryIterator->getFilename(), 0, -$cacheEntryFileExtensionLength));
333  } else {
334  $this->remove($directoryIterator->getFilename());
335  }
336  }
337  }
338  }
339 
348  protected function ‪findCacheFilesByIdentifier($entryIdentifier)
349  {
350  $pattern = $this->cacheDirectory . $entryIdentifier;
351  $filesFound = glob($pattern);
352  if (empty($filesFound)) {
353  return false;
354  }
355  return $filesFound;
356  }
357 
365  public function ‪requireOnce($entryIdentifier)
366  {
367  if ($this->frozen === true) {
368  if (isset($this->cacheEntryIdentifiers[$entryIdentifier])) {
369  return require_once $this->cacheDirectory . $entryIdentifier . ‪$this->cacheEntryFileExtension;
370  }
371  return false;
372  }
373  if ($entryIdentifier !== ‪PathUtility::basename($entryIdentifier)) {
374  throw new \InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1282073036);
375  }
376  $pathAndFilename = $this->cacheDirectory . $entryIdentifier . ‪$this->cacheEntryFileExtension;
377  return $this->‪isCacheFileExpired($pathAndFilename) ? false : require_once $pathAndFilename;
378  }
379 
387  public function require(string $entryIdentifier)
388  {
389  if ($this->frozen) {
390  if (isset($this->cacheEntryIdentifiers[$entryIdentifier])) {
391  return require $this->cacheDirectory . $entryIdentifier . ‪$this->cacheEntryFileExtension;
392  }
393  return false;
394  }
395  if ($entryIdentifier !== ‪PathUtility::basename($entryIdentifier)) {
396  throw new \InvalidArgumentException('The specified entry identifier must not contain a path segment.', 1532528246);
397  }
398  $pathAndFilename = $this->cacheDirectory . $entryIdentifier . ‪$this->cacheEntryFileExtension;
399  return $this->‪isCacheFileExpired($pathAndFilename) ? false : require $pathAndFilename;
400  }
401 }
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\requireOnce
‪mixed requireOnce($entryIdentifier)
Definition: FileBackend.php:362
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\EXPIRYTIME_LENGTH
‪const EXPIRYTIME_LENGTH
Definition: FileBackend.php:33
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$cache
‪FrontendInterface $cache
Definition: AbstractBackend.php:38
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\setCache
‪setCache(FrontendInterface $cache)
Definition: FileBackend.php:102
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\EXPIRYTIME_FORMAT
‪const EXPIRYTIME_FORMAT
Definition: FileBackend.php:32
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\has
‪bool has($entryIdentifier)
Definition: FileBackend.php:196
‪TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface
Definition: TaggableBackendInterface.php:22
‪TYPO3\CMS\Core\Cache\Backend\FreezableBackendInterface
Definition: FreezableBackendInterface.php:22
‪TYPO3\CMS\Core\Cache\Backend\FileBackend
Definition: FileBackend.php:30
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\isCacheFileExpired
‪bool isCacheFileExpired($cacheEntryPathAndFilename)
Definition: FileBackend.php:298
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\isFrozen
‪bool isFrozen()
Definition: FileBackend.php:88
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static basename(string $path)
Definition: PathUtility.php:219
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\collectGarbage
‪collectGarbage()
Definition: FileBackend.php:317
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\DATASIZE_DIGITS
‪const DATASIZE_DIGITS
Definition: FileBackend.php:34
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\flushByTag
‪flushByTag($tag)
Definition: FileBackend.php:280
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\$cacheEntryIdentifiers
‪array $cacheEntryIdentifiers
Definition: FileBackend.php:43
‪TYPO3\CMS\Core\Cache\Exception
Definition: DuplicateIdentifierException.php:16
‪TYPO3\CMS\Core\Cache\Exception\InvalidDataException
Definition: InvalidDataException.php:23
‪TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend
Definition: SimpleFileBackend.php:33
‪TYPO3\CMS\Core\Service\OpcodeCacheService
Definition: OpcodeCacheService.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixPermissions
‪static bool fixPermissions(string $path, bool $recursive=false)
Definition: GeneralUtility.php:1496
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\flush
‪flush()
Definition: FileBackend.php:267
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\findIdentifiersByTag
‪array findIdentifiersByTag($searchedTag)
Definition: FileBackend.php:231
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\$cacheEntryFileExtension
‪string $cacheEntryFileExtension
Definition: FileBackend.php:39
‪TYPO3\CMS\Core\Cache\Backend
Definition: AbstractBackend.php:16
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\findCacheFilesByIdentifier
‪mixed findCacheFilesByIdentifier($entryIdentifier)
Definition: FileBackend.php:345
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\$frozen
‪bool $frozen
Definition: FileBackend.php:47
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\SEPARATOR
‪const SEPARATOR
Definition: FileBackend.php:31
‪TYPO3\CMS\Core\Cache\Backend\FileBackend\freeze
‪freeze()
Definition: FileBackend.php:61
‪TYPO3\CMS\Core\Cache\Exception
Definition: Exception.php:21