1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Created by Fernando Robledo <[email protected]>. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Overdesign\PsrCache; |
8
|
|
|
|
9
|
|
|
use DateInterval; |
10
|
|
|
use DateTime; |
11
|
|
|
use DateTimeInterface; |
12
|
|
|
use Psr\Cache\CacheItemInterface; |
13
|
|
|
|
14
|
|
|
class CacheItem implements CacheItemInterface |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $key; |
18
|
|
|
/** @var bool */ |
19
|
|
|
protected $isHit; |
20
|
|
|
/** @var mixed */ |
21
|
|
|
protected $data; |
22
|
|
|
/** @var DateTime */ |
23
|
|
|
protected $expiresAt; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct($key, $data = null, $isHit = false) |
27
|
|
|
{ |
28
|
|
|
$this->key = $key; |
29
|
|
|
$this->data = $data; |
30
|
|
|
$this->isHit = $isHit; |
31
|
|
|
$this->expiresAt = null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
private function isExpired() |
38
|
|
|
{ |
39
|
|
|
if ($this->expiresAt === null) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$now = new DateTime(); |
44
|
|
|
|
45
|
|
|
return $now > $this->expiresAt; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the key for the current cache item. |
50
|
|
|
* |
51
|
|
|
* The key is loaded by the Implementing Library, but should be available to |
52
|
|
|
* the higher level callers when needed. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
* The key string for this cache item. |
56
|
|
|
*/ |
57
|
|
|
public function getKey() |
58
|
|
|
{ |
59
|
|
|
return $this->key; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Retrieves the value of the item from the cache associated with this object's key. |
64
|
|
|
* |
65
|
|
|
* The value returned must be identical to the value originally stored by set(). |
66
|
|
|
* |
67
|
|
|
* If isHit() returns false, this method MUST return null. Note that null |
68
|
|
|
* is a legitimate cached value, so the isHit() method SHOULD be used to |
69
|
|
|
* differentiate between "null value was found" and "no value was found." |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
* The value corresponding to this cache item's key, or null if not found. |
73
|
|
|
*/ |
74
|
|
|
public function get() |
75
|
|
|
{ |
76
|
|
|
return $this->isHit() ? $this->data : null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Confirms if the cache item lookup resulted in a cache hit. |
81
|
|
|
* |
82
|
|
|
* Note: This method MUST NOT have a race condition between calling isHit() |
83
|
|
|
* and calling get(). |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
* True if the request resulted in a cache hit. False otherwise. |
87
|
|
|
*/ |
88
|
|
|
public function isHit() |
89
|
|
|
{ |
90
|
|
|
return $this->isHit && !$this->isExpired(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets the value represented by this cache item. |
95
|
|
|
* |
96
|
|
|
* The $value argument may be any item that can be serialized by PHP, |
97
|
|
|
* although the method of serialization is left up to the Implementing |
98
|
|
|
* Library. |
99
|
|
|
* |
100
|
|
|
* @param mixed $value |
101
|
|
|
* The serializable value to be stored. |
102
|
|
|
* |
103
|
|
|
* @return static |
104
|
|
|
* The invoked object. |
105
|
|
|
*/ |
106
|
|
|
public function set($value) |
107
|
|
|
{ |
108
|
|
|
$this->data = $value; |
109
|
|
|
$this->isHit = true; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets the expiration time for this cache item. |
116
|
|
|
* |
117
|
|
|
* @param DateTimeInterface|null $expiration |
118
|
|
|
* The point in time after which the item MUST be considered expired. |
119
|
|
|
* If null is passed explicitly, a default value MAY be used. If none is set, |
120
|
|
|
* the value should be stored permanently or for as long as the |
121
|
|
|
* implementation allows. |
122
|
|
|
* |
123
|
|
|
* @return static |
124
|
|
|
* The called object. |
125
|
|
|
*/ |
126
|
|
|
public function expiresAt($expiration) |
127
|
|
|
{ |
128
|
|
|
if ($expiration === null || |
129
|
|
|
$expiration instanceof DateTime || // php < 5.5 |
130
|
|
|
$expiration instanceof DateTimeInterface) { |
|
|
|
|
131
|
|
|
$this->expiresAt = $expiration; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the expiration time for this cache item. |
139
|
|
|
* |
140
|
|
|
* @param int|\DateInterval|null $time |
141
|
|
|
* The period of time from the present after which the item MUST be considered |
142
|
|
|
* expired. An integer parameter is understood to be the time in seconds until |
143
|
|
|
* expiration. If null is passed explicitly, a default value MAY be used. |
144
|
|
|
* If none is set, the value should be stored permanently or for as long as the |
145
|
|
|
* implementation allows. |
146
|
|
|
* |
147
|
|
|
* @return static |
148
|
|
|
* The called object. |
149
|
|
|
*/ |
150
|
|
|
public function expiresAfter($time) |
151
|
|
|
{ |
152
|
|
|
if ($time === null) { |
153
|
|
|
$this->expiresAt = $time; |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$now = new DateTime(); |
158
|
|
|
$time = ($time instanceof DateInterval) ? $time : new DateInterval('PT' . $time . 'S'); |
159
|
|
|
|
160
|
|
|
$this->expiresAt = $now->add($time); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Updates isHit status when unserializing |
167
|
|
|
*/ |
168
|
|
|
public function __wakeup() |
169
|
|
|
{ |
170
|
|
|
$this->isHit = !$this->isExpired(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|