1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* PHP version 5.5 |
5
|
|
|
* |
6
|
|
|
* @package Cache |
7
|
|
|
* @author Sergey V.Kuzin <[email protected]> |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cache; |
12
|
|
|
|
13
|
|
|
use Psr\Cache\CacheItemInterface; |
14
|
|
|
|
15
|
|
|
class CacheItem implements CacheItemInterface |
16
|
|
|
{ |
17
|
|
|
protected $key; |
18
|
|
|
|
19
|
|
|
protected $value; |
20
|
|
|
|
21
|
|
|
protected $expiration; |
22
|
|
|
|
23
|
|
|
protected $hit; |
24
|
|
|
|
25
|
|
|
public function __construct($key, $ttl = null, $hit = false) |
26
|
|
|
{ |
27
|
|
|
$this->key = $key; |
28
|
|
|
$this->setExpiration($ttl); |
29
|
|
|
|
30
|
|
|
$this->setHit($hit); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the key for the current cache item. |
36
|
|
|
* |
37
|
|
|
* The key is loaded by the Implementing Library, but should be available to |
38
|
|
|
* the higher level callers when needed. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
* The key string for this cache item. |
42
|
|
|
*/ |
43
|
|
|
public function getKey() |
44
|
|
|
{ |
45
|
|
|
return $this->key; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Retrieves the value of the item from the cache associated with this objects key. |
50
|
|
|
* |
51
|
|
|
* The value returned must be identical to the value original stored by set(). |
52
|
|
|
* |
53
|
|
|
* if isHit() returns false, this method MUST return null. Note that null |
54
|
|
|
* is a legitimate cached value, so the isHit() method SHOULD be used to |
55
|
|
|
* differentiate between "null value was found" and "no value was found." |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
* The value corresponding to this cache item's key, or null if not found. |
59
|
|
|
*/ |
60
|
|
|
public function get() |
61
|
|
|
{ |
62
|
|
|
return $this->value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sets the value represented by this cache item. |
67
|
|
|
* |
68
|
|
|
* The $value argument may be any item that can be serialized by PHP, |
69
|
|
|
* although the method of serialization is left up to the Implementing |
70
|
|
|
* Library. |
71
|
|
|
* |
72
|
|
|
* Implementing Libraries MAY provide a default TTL if one is not specified. |
73
|
|
|
* If no TTL is specified and no default TTL has been set, the TTL MUST |
74
|
|
|
* be set to the maximum possible duration of the underlying storage |
75
|
|
|
* mechanism, or permanent if possible. |
76
|
|
|
* |
77
|
|
|
* @param mixed $value |
78
|
|
|
* The serializable value to be stored. |
79
|
|
|
* @param int|\DateTime $ttl |
80
|
|
|
* - If an integer is passed, it is interpreted as the number of seconds |
81
|
|
|
* after which the item MUST be considered expired. |
82
|
|
|
* - If a DateTime object is passed, it is interpreted as the point in |
83
|
|
|
* time after which the item MUST be considered expired. |
84
|
|
|
* - If no value is passed, a default value MAY be used. If none is set, |
85
|
|
|
* the value should be stored permanently or for as long as the |
86
|
|
|
* implementation allows. |
87
|
|
|
* @return static |
88
|
|
|
* The invoked object. |
89
|
|
|
*/ |
90
|
|
|
public function set($value, $ttl = null) |
91
|
|
|
{ |
92
|
|
|
$this->value = $value; |
93
|
|
|
$this->setExpiration($ttl); |
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Confirms if the cache item lookup resulted in a cache hit. |
99
|
|
|
* |
100
|
|
|
* Note: This method MUST NOT have a race condition between calling isHit() |
101
|
|
|
* and calling get(). |
102
|
|
|
* |
103
|
|
|
* @return boolean |
104
|
|
|
* True if the request resulted in a cache hit. False otherwise. |
105
|
|
|
*/ |
106
|
|
|
public function isHit() |
107
|
|
|
{ |
108
|
|
|
return $this->hit; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Confirms if the cache item exists in the cache. |
113
|
|
|
* |
114
|
|
|
* Note: This method MAY avoid retrieving the cached value for performance |
115
|
|
|
* reasons, which could result in a race condition between exists() and get(). |
116
|
|
|
* To avoid that potential race condition use isHit() instead. |
117
|
|
|
* |
118
|
|
|
* @return boolean |
119
|
|
|
* True if item exists in the cache, false otherwise. |
120
|
|
|
*/ |
121
|
|
|
public function exists() |
122
|
|
|
{ |
123
|
|
|
return $this->isHit(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sets the expiration time this cache item. |
128
|
|
|
* |
129
|
|
|
* @param \DateTime|\DateTimeImmutable $expiration |
130
|
|
|
* The point in time after which the item MUST be considered expired. |
131
|
|
|
* If null is passed explicitly, a default value MAY be used. If none is set, |
132
|
|
|
* the value should be stored permanently or for as long as the |
133
|
|
|
* implementation allows. |
134
|
|
|
* |
135
|
|
|
* @return static |
136
|
|
|
* The called object. |
137
|
|
|
*/ |
138
|
|
|
public function expiresAt($expiration) |
139
|
|
|
{ |
140
|
|
|
if ($expiration instanceof \DateTime) { |
141
|
|
|
$this->expiration = $expiration; |
142
|
|
|
} elseif ($expiration instanceof \DateTimeImmutable) { |
|
|
|
|
143
|
|
|
$this->expiration = $this->expiration = $expiration; |
144
|
|
|
} else { |
145
|
|
|
throw new InvalidArgumentException(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets the expiration time this cache item. |
153
|
|
|
* |
154
|
|
|
* @param int|\DateInterval $time |
155
|
|
|
* The period of time from the present after which the item MUST be considered |
156
|
|
|
* expired. An integer parameter is understood to be the time in seconds until |
157
|
|
|
* expiration. |
158
|
|
|
* |
159
|
|
|
* @return static |
160
|
|
|
* The called object. |
161
|
|
|
*/ |
162
|
|
|
public function expiresAfter($time) |
163
|
|
|
{ |
164
|
|
|
if (true === is_numeric($time)) { |
165
|
|
|
$this->expiration = new \DateTime('now +' . $time . ' seconds'); |
166
|
|
|
} elseif ($time instanceof \DateInterval) { |
167
|
|
|
$this->expiration = new \DateTime('now +' . $time->format('U') . ' seconds'); |
168
|
|
|
} else { |
169
|
|
|
throw new InvalidArgumentException(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the expiration time of a not-yet-expired cache item. |
177
|
|
|
* |
178
|
|
|
* If this cache item is a Cache Miss, this method MAY return the time at |
179
|
|
|
* which the item expired or the current time if that is not available. |
180
|
|
|
* |
181
|
|
|
* @return \DateTime |
182
|
|
|
* The timestamp at which this cache item will expire. |
183
|
|
|
*/ |
184
|
|
|
public function getExpiration() |
185
|
|
|
{ |
186
|
|
|
return $this->expiration; |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param null $ttl |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
public function setExpiration($ttl = null) |
194
|
|
|
{ |
195
|
|
|
if (true === is_numeric($ttl)) { |
196
|
|
|
$this->expiration = new \DateTime('now +' . $ttl . ' seconds'); |
197
|
|
|
} elseif ($ttl instanceof \DateTime) { |
198
|
|
|
$this->expiration = $ttl; |
199
|
|
|
} elseif (null === $ttl) { |
200
|
|
|
$this->expiration = new \DateTime('now + 10 years'); |
201
|
|
|
} else { |
202
|
|
|
throw new InvalidArgumentException(); |
203
|
|
|
} |
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param boolean $hit |
209
|
|
|
* |
210
|
|
|
* @return CacheItem |
211
|
|
|
* @throws InvalidArgumentException not a boolean value given |
212
|
|
|
*/ |
213
|
|
|
public function setHit($hit) |
214
|
|
|
{ |
215
|
|
|
/* if (false === is_bool($hit)) { |
|
|
|
|
216
|
|
|
throw InvalidArgumentException::typeMismatch($hit, 'Boolean'); |
217
|
|
|
}*/ |
218
|
|
|
$this->hit = $hit; |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.