1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Cache;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use Psr\Cache\CacheItemInterface;
|
|
|
|
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Class Item
|
22
|
|
|
*
|
23
|
|
|
* @package O2System\Cache\Collections
|
24
|
|
|
*/
|
25
|
|
|
class Item implements CacheItemInterface
|
26
|
|
|
{
|
27
|
|
|
/**
|
28
|
|
|
* Item::$key
|
29
|
|
|
*
|
30
|
|
|
* Item Key
|
31
|
|
|
*
|
32
|
|
|
* @var string
|
33
|
|
|
*/
|
34
|
|
|
protected $key;
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* Item::$value
|
38
|
|
|
*
|
39
|
|
|
* Item Value
|
40
|
|
|
*
|
41
|
|
|
* @var mixed|null
|
42
|
|
|
*/
|
43
|
|
|
protected $value = null;
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* Item::$isHit
|
47
|
|
|
*
|
48
|
|
|
* Item is hit flag
|
49
|
|
|
*
|
50
|
|
|
* @var bool
|
51
|
|
|
*/
|
52
|
|
|
protected $isHit = false;
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* Item::$createdAt
|
56
|
|
|
*
|
57
|
|
|
* Item Creation Time
|
58
|
|
|
*
|
59
|
|
|
* @var \DateTimeInterface
|
60
|
|
|
*/
|
61
|
|
|
protected $createdAt;
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* Item::$expiresAt
|
65
|
|
|
*
|
66
|
|
|
* Item Expiration Time
|
67
|
|
|
*
|
68
|
|
|
* @var \DateTimeInterface
|
69
|
|
|
*/
|
70
|
|
|
protected $expiresAt;
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* Item::$expiresAfter
|
74
|
|
|
*
|
75
|
|
|
* Item Expiration Time Interval
|
76
|
|
|
*
|
77
|
|
|
* @var int|\DateInterval
|
78
|
|
|
*/
|
79
|
|
|
protected $expiresAfter;
|
80
|
|
|
|
81
|
|
|
// ------------------------------------------------------------------------
|
82
|
|
|
|
83
|
|
|
/**
|
84
|
|
|
* Item::__construct
|
85
|
|
|
*
|
86
|
|
|
* @param string $key Item key.
|
87
|
|
|
* @param mixed|null $value Item value.
|
88
|
|
|
* @param int|\DateInterval $expiresAfter Item expiration.
|
89
|
|
|
*
|
90
|
|
|
* @throws \Exception
|
91
|
|
|
* @return Item
|
92
|
|
|
*/
|
93
|
|
|
public function __construct($key, $value = null, $expiresAfter = 300)
|
94
|
|
|
{
|
95
|
|
|
// Set item key
|
96
|
|
|
$this->key = $key;
|
97
|
|
|
|
98
|
|
|
// Set from item metadata
|
99
|
|
|
if (isset($value[ 'ctime' ]) AND isset($value[ 'etime' ]) AND isset($value[ 'ttl' ]) AND isset($value[ 'data' ])) {
|
100
|
|
|
$this->set($value[ 'data' ]);
|
101
|
|
|
$this->createdAt = new \DateTime(date('r', $value[ 'ctime' ]));
|
102
|
|
|
$this->expiresAt = new \DateTime(date('r', $value[ 'etime' ]));
|
103
|
|
|
$this->expiresAfter($value[ 'ttl' ]);
|
104
|
|
|
} else {
|
105
|
|
|
// Set item value
|
106
|
|
|
$this->set($value);
|
107
|
|
|
|
108
|
|
|
// Set item creation time
|
109
|
|
|
$this->createdAt = new \DateTime(date('r', time()));
|
110
|
|
|
|
111
|
|
|
// Set item expiration
|
112
|
|
|
if ($expiresAfter !== false) {
|
|
|
|
|
113
|
|
|
$this->expiresAfter($expiresAfter);
|
114
|
|
|
}
|
115
|
|
|
}
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
// ------------------------------------------------------------------------
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* Item::set
|
122
|
|
|
*
|
123
|
|
|
* Sets the value represented by this cache item.
|
124
|
|
|
*
|
125
|
|
|
* The $value argument may be any item that can be serialized by PHP,
|
126
|
|
|
* although the method of serialization is left up to the Implementing
|
127
|
|
|
* Library.
|
128
|
|
|
*
|
129
|
|
|
* @param mixed $value
|
130
|
|
|
* The serializable value to be stored.
|
131
|
|
|
*
|
132
|
|
|
* @return static
|
133
|
|
|
* The invoked object.
|
134
|
|
|
*/
|
135
|
|
|
public function set($value)
|
136
|
|
|
{
|
137
|
|
|
$this->value = $value;
|
138
|
|
|
|
139
|
|
|
if ( ! is_null($value)) {
|
140
|
|
|
$this->isHit = true;
|
141
|
|
|
}
|
142
|
|
|
|
143
|
|
|
return $this;
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
// ------------------------------------------------------------------------
|
147
|
|
|
|
148
|
|
|
/**
|
149
|
|
|
* Item::expiresAfter
|
150
|
|
|
*
|
151
|
|
|
* Sets the expiration time for this cache item.
|
152
|
|
|
*
|
153
|
|
|
* @param int|\DateInterval|null $time
|
154
|
|
|
* The period of time from the present after which the item MUST be considered
|
155
|
|
|
* expired. An integer parameter is understood to be the time in seconds until
|
156
|
|
|
* expiration. If null is passed explicitly, a default value MAY be used.
|
157
|
|
|
* If none is set, the value should be stored permanently or for as long as the
|
158
|
|
|
* implementation allows.
|
159
|
|
|
*
|
160
|
|
|
* @return static
|
161
|
|
|
* The called object.
|
162
|
|
|
* @throws \Exception
|
163
|
|
|
*/
|
164
|
|
|
public function expiresAfter($time = null)
|
165
|
|
|
{
|
166
|
|
|
$time = is_null($time) ? 300 : $time;
|
167
|
|
|
|
168
|
|
|
if (is_int($time)) {
|
169
|
|
|
$this->expiresAfter = new \DateInterval('PT' . $time . 'S');
|
170
|
|
|
}
|
171
|
|
|
|
172
|
|
|
if ( ! $this->expiresAt instanceof \DateTime) {
|
173
|
|
|
$this->expiresAt();
|
174
|
|
|
}
|
175
|
|
|
|
176
|
|
|
$this->expiresAt->add($this->expiresAfter);
|
177
|
|
|
|
178
|
|
|
return $this;
|
179
|
|
|
}
|
180
|
|
|
|
181
|
|
|
// ------------------------------------------------------------------------
|
182
|
|
|
|
183
|
|
|
/**
|
184
|
|
|
* Item::expiresAt
|
185
|
|
|
*
|
186
|
|
|
* Sets the expiration time for this cache item.
|
187
|
|
|
*
|
188
|
|
|
* @param \DateTimeInterface|null $expiration
|
189
|
|
|
* The point in time after which the item MUST be considered expired.
|
190
|
|
|
* If null is passed explicitly, a default value MAY be used. If none is set,
|
191
|
|
|
* the value should be stored permanently or for as long as the
|
192
|
|
|
* implementation allows.
|
193
|
|
|
*
|
194
|
|
|
* @return static
|
195
|
|
|
* The called object.
|
196
|
|
|
* @throws \Exception
|
197
|
|
|
*/
|
198
|
|
|
public function expiresAt($expiration = null)
|
199
|
|
|
{
|
200
|
|
|
$this->expiresAt = isset($expiration) ? $expiration : new \DateTime();
|
201
|
|
|
|
202
|
|
|
return $this;
|
203
|
|
|
}
|
204
|
|
|
|
205
|
|
|
// ------------------------------------------------------------------------
|
206
|
|
|
|
207
|
|
|
/**
|
208
|
|
|
* Item::getKey
|
209
|
|
|
*
|
210
|
|
|
* Returns the key for the current cache item.
|
211
|
|
|
*
|
212
|
|
|
* The key is loaded by the Implementing Library, but should be available to
|
213
|
|
|
* the higher level callers when needed.
|
214
|
|
|
*
|
215
|
|
|
* @return string
|
216
|
|
|
* The key string for this cache item.
|
217
|
|
|
*/
|
218
|
|
|
public function getKey()
|
219
|
|
|
{
|
220
|
|
|
return $this->key;
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
// ------------------------------------------------------------------------
|
224
|
|
|
|
225
|
|
|
/**
|
226
|
|
|
* Item::get
|
227
|
|
|
*
|
228
|
|
|
* Retrieves the value of the item from the cache associated with this object's key.
|
229
|
|
|
*
|
230
|
|
|
* The value returned must be identical to the value originally stored by set().
|
231
|
|
|
*
|
232
|
|
|
* If isHit() returns false, this method MUST return null. Note that null
|
233
|
|
|
* is a legitimate cached value, so the isHit() method SHOULD be used to
|
234
|
|
|
* differentiate between "null value was found" and "no value was found."
|
235
|
|
|
*
|
236
|
|
|
* @return mixed
|
237
|
|
|
* The value corresponding to this cache item's key, or null if not found.
|
238
|
|
|
*/
|
239
|
|
|
public function get()
|
240
|
|
|
{
|
241
|
|
|
if ($this->isHit()) {
|
242
|
|
|
return $this->value;
|
243
|
|
|
}
|
244
|
|
|
|
245
|
|
|
return null;
|
246
|
|
|
}
|
247
|
|
|
|
248
|
|
|
// ------------------------------------------------------------------------
|
249
|
|
|
|
250
|
|
|
/**
|
251
|
|
|
* Item::isHit
|
252
|
|
|
*
|
253
|
|
|
* Confirms if the cache item lookup resulted in a cache hit.
|
254
|
|
|
*
|
255
|
|
|
* Note: This method MUST NOT have a race condition between calling isHit()
|
256
|
|
|
* and calling get().
|
257
|
|
|
*
|
258
|
|
|
* @return bool
|
259
|
|
|
* True if the request resulted in a cache hit. False otherwise.
|
260
|
|
|
*/
|
261
|
|
|
public function isHit()
|
262
|
|
|
{
|
263
|
|
|
return $this->isHit;
|
264
|
|
|
}
|
265
|
|
|
|
266
|
|
|
// ------------------------------------------------------------------------
|
267
|
|
|
|
268
|
|
|
/**
|
269
|
|
|
* Item::getMetadata
|
270
|
|
|
*
|
271
|
|
|
* Gets item metadata info in array format:
|
272
|
|
|
* [
|
273
|
|
|
* 'ctime' => 1474609788, // creation time
|
274
|
|
|
* 'etime' => '1474610088', // expires time
|
275
|
|
|
* 'ttl' => 300 // time-to-live
|
276
|
|
|
* ]
|
277
|
|
|
*
|
278
|
|
|
* @return array
|
279
|
|
|
*/
|
280
|
|
|
public function getMetadata()
|
281
|
|
|
{
|
282
|
|
|
$createdTime = $this->createdAt->format('U');
|
283
|
|
|
|
284
|
|
|
if ($this->expiresAt instanceof \DateTime) {
|
285
|
|
|
$expiresTime = $this->expiresAt->format('U');
|
286
|
|
|
$ttl = $expiresTime - $createdTime;
|
287
|
|
|
} else {
|
288
|
|
|
$expiresTime = 0;
|
289
|
|
|
$ttl = 0;
|
290
|
|
|
}
|
291
|
|
|
|
292
|
|
|
return [
|
293
|
|
|
'ctime' => $createdTime,
|
294
|
|
|
'etime' => $expiresTime,
|
295
|
|
|
'ttl' => $ttl,
|
296
|
|
|
];
|
297
|
|
|
}
|
298
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths