1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\Cache; |
19
|
|
|
|
20
|
|
|
use Biurad\Cache\Exceptions\InvalidArgumentException; |
21
|
|
|
use Psr\Cache\CacheItemInterface; |
22
|
|
|
|
23
|
|
|
final class CacheItem implements CacheItemInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Reserved characters that cannot be used in a key or tag. |
27
|
|
|
*/ |
28
|
|
|
public const RESERVED_CHARACTERS = '{}()/\@:'; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $key; |
32
|
|
|
|
33
|
|
|
/** @var mixed */ |
34
|
|
|
private $value; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
private $isHit = false; |
38
|
|
|
|
39
|
|
|
/** @var float|int|null */ |
40
|
|
|
private $expiry; |
41
|
|
|
|
42
|
|
|
/** @var int */ |
43
|
|
|
private $defaultLifetime; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function getKey(): string |
49
|
|
|
{ |
50
|
1 |
|
return $this->key; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
2 |
|
public function get() |
57
|
|
|
{ |
58
|
2 |
|
return $this->value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function isHit(): bool |
65
|
|
|
{ |
66
|
1 |
|
return $this->isHit; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
2 |
|
public function set($value) |
73
|
|
|
{ |
74
|
2 |
|
$this->value = $value; |
75
|
|
|
|
76
|
2 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
2 |
|
public function expiresAt($expiration) |
83
|
|
|
{ |
84
|
2 |
|
if (null === $expiration) { |
85
|
1 |
|
return $this->setDefaultExpiration(); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
if (!$expiration instanceof \DateTimeInterface) { |
89
|
1 |
|
throw new InvalidArgumentException('Expiration date must implement DateTimeInterface or be null.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$this->expiry = (float) $expiration->format('U.u'); |
93
|
|
|
|
94
|
1 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
1 |
|
public function expiresAfter($time) |
101
|
|
|
{ |
102
|
1 |
|
if (null === $time) { |
103
|
1 |
|
return $this->setDefaultExpiration(); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
if ($time instanceof \DateInterval) { |
107
|
1 |
|
$interval = \DateTime::createFromFormat('U', '0')->add($time); |
108
|
1 |
|
$this->expiry = \microtime(true) + (int) $interval->format('U.u'); |
109
|
1 |
|
} elseif (\is_int($time)) { |
110
|
1 |
|
$this->expiry = $time + \microtime(true); |
111
|
|
|
} else { |
112
|
|
|
throw new InvalidArgumentException('Expiration date must be an integer, a DateInterval or null.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @internal |
120
|
|
|
*/ |
121
|
|
|
public function getExpiry(): ?float |
122
|
|
|
{ |
123
|
|
|
return $this->expiry; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return static |
128
|
|
|
*/ |
129
|
2 |
|
private function setDefaultExpiration(): self |
130
|
|
|
{ |
131
|
2 |
|
$this->expiry = $this->defaultLifetime > 0 ? \microtime(true) + $this->defaultLifetime : null; |
132
|
|
|
|
133
|
2 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|