‪TYPO3CMS  9.5
AbstractBackend.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 
17 use Psr\Log\LoggerAwareInterface;
18 use Psr\Log\LoggerAwareTrait;
21 
25 abstract class ‪AbstractBackend implements ‪BackendInterface, LoggerAwareInterface
26 {
27  use LoggerAwareTrait;
28 
29  const ‪DATETIME_EXPIRYTIME_UNLIMITED = '9999-12-31T23:59:59+0000';
36  protected ‪$cache;
37 
41  protected ‪$cacheIdentifier;
42 
51  protected ‪$context;
52 
58  protected ‪$defaultLifetime = 3600;
59 
67  public function ‪__construct(‪$context, array $options = [])
68  {
69  $this->context = ‪$context;
70  if (is_array($options) || $options instanceof \ArrayAccess) {
71  foreach ($options as $optionKey => $optionValue) {
72  $methodName = 'set' . ucfirst($optionKey);
73  if (method_exists($this, $methodName)) {
74  $this->{$methodName}($optionValue);
75  } else {
76  throw new \InvalidArgumentException('Invalid cache backend option "' . $optionKey . '" for backend of type "' . static::class . '"', 1231267498);
77  }
78  }
79  }
80  if ($this->logger === null) {
81  $this->setLogger(
82  GeneralUtility::makeInstance(LogManager::class)
83  ->getLogger(static::class)
84  );
85  }
86  }
87 
93  public function ‪setCache(\‪TYPO3\CMS\Core\Cache\Frontend\‪FrontendInterface ‪$cache)
94  {
95  $this->cache = ‪$cache;
96  $this->cacheIdentifier = $this->cache->‪getIdentifier();
97  }
98 
106  {
107  if (!is_int(‪$defaultLifetime) || ‪$defaultLifetime < 0) {
108  throw new \InvalidArgumentException('The default lifetime must be given as a positive integer.', 1233072774);
109  }
110  $this->defaultLifetime = ‪$defaultLifetime;
111  }
112 
123  public function ‪flushByTags(array $tags)
124  {
125  array_walk($tags, [$this, 'flushByTag']);
126  }
127 
135  protected function ‪calculateExpiryTime($lifetime = null)
136  {
137  if ($lifetime === self::UNLIMITED_LIFETIME || $lifetime === null && $this->defaultLifetime === self::UNLIMITED_LIFETIME) {
138  $expiryTime = new \DateTime(self::DATETIME_EXPIRYTIME_UNLIMITED, new \DateTimeZone('UTC'));
139  } else {
140  if ($lifetime === null) {
141  $lifetime = ‪$this->defaultLifetime;
142  }
143  $expiryTime = new \DateTime('now +' . $lifetime . ' seconds', new \DateTimeZone('UTC'));
144  }
145  return $expiryTime;
146  }
147 }
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\calculateExpiryTime
‪DateTime calculateExpiryTime($lifetime=null)
Definition: AbstractBackend.php:131
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\UNLIMITED_LIFETIME
‪const UNLIMITED_LIFETIME
Definition: AbstractBackend.php:30
‪TYPO3
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\__construct
‪__construct($context, array $options=[])
Definition: AbstractBackend.php:63
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$defaultLifetime
‪int $defaultLifetime
Definition: AbstractBackend.php:54
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\flushByTags
‪flushByTags(array $tags)
Definition: AbstractBackend.php:119
‪TYPO3\CMS\Core\Cache\Backend\BackendInterface
Definition: BackendInterface.php:23
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$cache
‪TYPO3 CMS Core Cache Frontend FrontendInterface $cache
Definition: AbstractBackend.php:35
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\setDefaultLifetime
‪setDefaultLifetime($defaultLifetime)
Definition: AbstractBackend.php:101
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\DATETIME_EXPIRYTIME_UNLIMITED
‪const DATETIME_EXPIRYTIME_UNLIMITED
Definition: AbstractBackend.php:29
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\$cacheIdentifier
‪string $cacheIdentifier
Definition: AbstractBackend.php:39
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend
Definition: AbstractBackend.php:26
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:25
‪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\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Cache\Backend\AbstractBackend\setCache
‪setCache(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache)
Definition: AbstractBackend.php:89