1
|
|
|
<?php namespace Comodojo\Cache; |
2
|
|
|
|
3
|
|
|
use \Psr\Cache\CacheItemInterface; |
4
|
|
|
use \Comodojo\Cache\Components\KeyValidator; |
5
|
|
|
use \DateTimeInterface; |
6
|
|
|
use \DateTime; |
7
|
|
|
use \DateInterval; |
8
|
|
|
use \Comodojo\Exception\InvalidCacheArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package Comodojo Cache |
12
|
|
|
* @author Marco Giovinazzi <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* |
15
|
|
|
* LICENSE: |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
* THE SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
class Item implements CacheItemInterface { |
27
|
|
|
|
28
|
|
|
protected $key; |
29
|
|
|
|
30
|
|
|
protected $data; |
31
|
|
|
|
32
|
|
|
protected $hit = false; |
33
|
|
|
|
34
|
|
|
protected $expiration = 0; |
35
|
|
|
|
36
|
196 |
|
public function __construct($key, $hit = false) { |
37
|
|
|
|
38
|
196 |
|
if ( KeyValidator::validateKey($key) === false ) { |
39
|
11 |
|
throw new InvalidCacheArgumentException('Invalid key provided'); |
40
|
|
|
} |
41
|
|
|
|
42
|
185 |
|
$this->key = $key; |
43
|
|
|
|
44
|
185 |
|
$this->hit = $hit; |
45
|
|
|
|
46
|
185 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
129 |
|
public function getKey() { |
52
|
|
|
|
53
|
129 |
|
return $this->key; |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
154 |
|
public function get() { |
61
|
|
|
|
62
|
154 |
|
return $this->isHit() ? $this->data : null; |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
164 |
|
public function isHit() { |
70
|
|
|
|
71
|
|
|
// return !is_null($this->data); |
72
|
164 |
|
return $this->hit === true; |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
172 |
|
public function set($value) { |
80
|
|
|
|
81
|
172 |
|
$this->data = $value; |
82
|
|
|
|
83
|
172 |
|
return $this; |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
57 |
|
public function expiresAt($expiration = null) { |
91
|
|
|
|
92
|
57 |
|
if ( is_null($expiration) ) { |
93
|
|
|
$this->expiration = 0; |
94
|
|
|
} |
95
|
|
|
|
96
|
57 |
|
if ( $expiration instanceof DateTimeInterface ) { |
97
|
57 |
|
$this->expiration = $expiration; |
98
|
|
|
} |
99
|
|
|
|
100
|
57 |
|
return $this; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
25 |
|
public function expiresAfter($time) { |
108
|
|
|
|
109
|
25 |
|
if ( is_null($time) ) { |
110
|
|
|
$this->expiration = 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
25 |
|
if ( is_numeric($time) ) { |
114
|
25 |
|
$this->expiration = new DateTime('now +'.$time.' seconds'); |
115
|
|
|
} |
116
|
|
|
|
117
|
25 |
|
if ( $time instanceof DateInterval ) { |
118
|
|
|
$expiration = new DateTime('now'); |
119
|
|
|
$expiration->add($time); |
120
|
|
|
$this->expiration = $expiration; |
121
|
|
|
} |
122
|
|
|
|
123
|
25 |
|
return $this; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the raw value, regardless of hit status. |
129
|
|
|
* |
130
|
|
|
* Although not part of the CacheItemInterface, this method is used by |
131
|
|
|
* the pool for extracting information for saving. |
132
|
|
|
* |
133
|
|
|
* @return mixed |
134
|
|
|
* |
135
|
|
|
* @internal |
136
|
|
|
*/ |
137
|
122 |
|
public function getRaw() { |
138
|
|
|
|
139
|
122 |
|
return $this->data; |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get currently (calculated) ttl of cache item |
145
|
|
|
* |
146
|
|
|
* This method is not part of the CacheItemInterface. |
147
|
|
|
* |
148
|
|
|
* @return int |
149
|
|
|
* |
150
|
|
|
* @internal |
151
|
|
|
*/ |
152
|
170 |
|
public function getTtl() { |
153
|
|
|
|
154
|
170 |
|
if ( is_null($this->expiration) ) return null; |
|
|
|
|
155
|
|
|
|
156
|
170 |
|
if ( $this->expiration === 0 ) return 0; |
157
|
|
|
|
158
|
96 |
|
$now = new DateTime("now"); |
159
|
|
|
|
160
|
96 |
|
if ( $now > $this->expiration ) return -1; |
161
|
|
|
|
162
|
47 |
|
return (int) $now->diff($this->expiration)->format("%r%s"); |
|
|
|
|
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get expiration time (absolute) |
168
|
|
|
* |
169
|
|
|
* This method is not part of the CacheItemInterface. |
170
|
|
|
* |
171
|
|
|
* @return int |
172
|
|
|
* |
173
|
|
|
* @internal |
174
|
|
|
*/ |
175
|
1 |
|
public function getExpiration() { |
176
|
|
|
|
177
|
1 |
|
return $this->expiration; |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
public function __toString() { |
182
|
|
|
|
183
|
1 |
|
return serialize($this->get()); |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
} |
188
|
|
|
|