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
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Cache\Psr\CacheItemPool;
use DateInterval;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Psr\Cache\CacheItemInterface;
final class CacheItem implements CacheItemInterface
{
/**
* Cache key
* @var string
*/
private $key;
/**
* Cache value
* @var mixed|null
*/
private $value;
/**
* True if the cache item lookup resulted in a cache hit or if they item is deferred or successfully saved
* @var bool
*/
private $isHit = false;
/**
* Timestamp item will expire at if expiresAt() called, null otherwise
* @var int|null
*/
private $expiration = null;
/**
* Seconds after item is stored it will expire at if expiresAfter() called, null otherwise
* @var int|null
*/
private $ttl = null;
/**
* @var DateTimeZone
*/
private $tz;
/**
* Constructor.
*
* @param string $key
* @param mixed $value
* @param bool $isHit
*/
public function __construct($key, $value, $isHit)
{
$this->key = $key;
$this->value = $isHit ? $value : null;
$this->isHit = $isHit;
$this->utc = new DateTimeZone('UTC');
}
/**
* {@inheritdoc}
*/
public function getKey()
{
return $this->key;
}
/**
* {@inheritdoc}
*/
public function get()
{
return $this->value;
}
/**
* {@inheritdoc}
*/
public function isHit()
{
if (! $this->isHit) {
return false;
}
$ttl = $this->getTtl();
return $ttl === null || $ttl > 0;
}
/**
* Sets isHit value
*
* This function is called by CacheItemPoolDecorator::saveDeferred() and is not intended for use by other calling
* code.
*
* @param boolean $isHit
* @return $this
*/
public function setIsHit($isHit)
{
$this->isHit = $isHit;
return $this;
}
/**
* {@inheritdoc}
*/
public function set($value)
{
$this->value = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function expiresAt($expiration)
{
if (! ($expiration === null || $expiration instanceof DateTimeInterface)) {
throw new InvalidArgumentException('$expiration must be null or an instance of DateTimeInterface');
}
$this->expiration = $expiration instanceof DateTimeInterface ? $expiration->getTimestamp() : null;
$this->ttl = null;
return $this;
}
/**
* {@inheritdoc}
*/
public function expiresAfter($time)
{
if ($time instanceof DateInterval) {
$now = new DateTimeImmutable('now', $this->utc);
$end = $now->add($time);
$this->ttl = $end->getTimestamp() - $now->getTimestamp();
} elseif (is_int($time) || $time === null) {
$this->ttl = $time;
} else {
throw new InvalidArgumentException(sprintf('Invalid $time "%s"', gettype($time)));
}
$this->expiration = null;
return $this;
}
/**
* Returns number of seconds until item expires
*
* If NULL, the pool should use the default TTL for the storage adapter. If <= 0, the item has expired.
*
* @return int|null
*/
public function getTtl()
{
$ttl = $this->ttl;
if ($this->expiration !== null) {
$ttl = $this->expiration - time();
}
return $ttl;
}
}