-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
AbstractBackend.php
179 lines (159 loc) · 5.71 KB
/
AbstractBackend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Neos\Cache\Backend;
/*
* This file is part of the Neos.Cache package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Cache\EnvironmentConfiguration;
use Neos\Cache\Frontend\LowLevelFrontendInterface;
/**
* An abstract caching backend
*
* @api
*/
abstract class AbstractBackend implements BackendInterface
{
const DATETIME_EXPIRYTIME_UNLIMITED = '9999-12-31T23:59:59+0000';
const UNLIMITED_LIFETIME = 0;
/**
* Reference to the cache frontend which uses this backend
* @var LowLevelFrontendInterface
*/
protected $cache;
/**
* @var string
*/
protected $cacheIdentifier;
/**
* A prefix to seperate stored by appliaction context and cache
* @var string
*/
protected $identifierPrefix;
/**
* Default lifetime of a cache entry in seconds
* @var integer
*/
protected $defaultLifetime = 3600;
/**
* @var EnvironmentConfiguration
*/
protected $environmentConfiguration;
/**
* Constructs this backend
*
* @param EnvironmentConfiguration $environmentConfiguration
* @param array $options Configuration options - depends on the actual backend
* @api
*/
public function __construct(EnvironmentConfiguration $environmentConfiguration = null, array $options = [])
{
$this->environmentConfiguration = $environmentConfiguration;
if (is_array($options) || $options instanceof \Iterator) {
$this->setProperties($options);
}
}
/**
* @param array $properties
* @param boolean $throwExceptionIfPropertyNotSettable
* @return void
* @throws \InvalidArgumentException
*/
protected function setProperties(array $properties, bool $throwExceptionIfPropertyNotSettable = true): void
{
foreach ($properties as $propertyName => $propertyValue) {
$propertyWasSet = $this->setProperty($propertyName, $propertyValue);
if ($propertyWasSet) {
continue;
}
if ($throwExceptionIfPropertyNotSettable) {
throw new \InvalidArgumentException('Invalid cache backend option "' . $propertyName . '" for backend of type "' . get_class($this) . '"', 1231267498);
}
}
}
/**
* @param string $propertyName
* @param mixed $propertyValue
* @return boolean
*/
protected function setProperty(string $propertyName, $propertyValue): bool
{
$setterName = 'set' . ucfirst($propertyName);
if (method_exists($this, $setterName)) {
$this->$setterName($propertyValue);
return true;
}
return false;
}
/**
* Sets a reference to the cache frontend which uses this backend
*
* @param LowLevelFrontendInterface $cache The frontend for this backend
* @return void
* @api
*/
public function setCache(LowLevelFrontendInterface $cache): void
{
$this->cache = $cache;
$this->cacheIdentifier = $this->cache->getIdentifier();
$applicationIdentifier = $this->environmentConfiguration instanceof EnvironmentConfiguration ? $this->environmentConfiguration->getApplicationIdentifier() : '';
$this->identifierPrefix = md5($applicationIdentifier) . ':' . $this->cacheIdentifier . ':';
}
/**
* Sets the default lifetime for this cache backend
*
* @param integer|string $defaultLifetime Default lifetime of this cache backend in seconds. 0 means unlimited lifetime.
* @return void
* @throws \InvalidArgumentException
* @api
*/
public function setDefaultLifetime($defaultLifetime): void
{
if ((int)$defaultLifetime < 0) {
throw new \InvalidArgumentException('The default lifetime must be given as a positive integer', 1233072774);
}
$this->defaultLifetime = (int)$defaultLifetime;
}
/**
* Calculates the expiry time by the given lifetime. If no lifetime is
* specified, the default lifetime is used.
*
* @param integer $lifetime The lifetime in seconds
* @return \DateTime The expiry time
*/
protected function calculateExpiryTime(int $lifetime = null): \DateTime
{
if ($lifetime === self::UNLIMITED_LIFETIME || ($lifetime === null && $this->defaultLifetime === self::UNLIMITED_LIFETIME)) {
return new \DateTime(self::DATETIME_EXPIRYTIME_UNLIMITED, new \DateTimeZone('UTC'));
}
if ($lifetime === null) {
$lifetime = $this->defaultLifetime;
}
return new \DateTime('now +' . $lifetime . ' seconds', new \DateTimeZone('UTC'));
}
/**
* Returns the internally used, prefixed entry identifier for the given public
* entry identifier.
*
* While Flow applications will mostly refer to the simple entry identifier, it
* may be necessary to know the actual identifier used by the cache backend
* in order to share cache entries with other applications. This method allows
* for retrieving it.
*
* Note that, in case of the AbstractBackend, this method is returns just the
* given entry identifier.
*
* @param string $entryIdentifier The short entry identifier, for example "NumberOfPostedArticles"
* @return string The prefixed identifier, for example "d59b7012de96aecf8171f8760323fe0a:Flow_Fusion_Content:NumberOfPostedArticles:"
* @api
*/
public function getPrefixedIdentifier(string $entryIdentifier): string
{
return $this->identifierPrefix . $entryIdentifier;
}
}