|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of the Joomla Framework Cache Package |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
|
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Joomla\Cache\Item; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Cache item instance for the Joomla Framework. |
|
13
|
|
|
* |
|
14
|
|
|
* @since 1.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class Item extends AbstractItem |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The time the object expires at |
|
20
|
|
|
* |
|
21
|
|
|
* @var \DateTimeInterface |
|
22
|
|
|
* @since __DEPLOY_VERSION__ |
|
23
|
|
|
*/ |
|
24
|
|
|
private $expiration; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The key for the cache item. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
* @since 1.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
private $key; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The value of the cache item. |
|
36
|
|
|
* |
|
37
|
|
|
* @var mixed |
|
38
|
|
|
* @since 1.0 |
|
39
|
|
|
*/ |
|
40
|
|
|
private $value; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Whether the cache item has been hit. |
|
44
|
|
|
* |
|
45
|
|
|
* @var boolean |
|
46
|
|
|
* @since 1.0 |
|
47
|
|
|
*/ |
|
48
|
|
|
private $hit = false; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Class constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $key The key for the cache item. |
|
54
|
|
|
* @param \DateInterval|\DateTimeInterface|integer|null $ttl The expiry time for the cache item in seconds or as a datetime object |
|
55
|
|
|
* |
|
56
|
|
|
* @since 1.0 |
|
57
|
|
|
*/ |
|
58
|
54 |
|
public function __construct($key, $ttl = null) |
|
59
|
|
|
{ |
|
60
|
54 |
|
$this->key = $key; |
|
61
|
|
|
|
|
62
|
54 |
|
if (is_int($ttl) || ($ttl instanceof \DateInterval)) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->expiresAfter($ttl); |
|
65
|
|
|
} |
|
66
|
54 |
|
elseif ($ttl instanceof \DateTimeInterface) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->expiresAt($ttl); |
|
69
|
|
|
} |
|
70
|
|
|
else |
|
71
|
|
|
{ |
|
72
|
54 |
|
$this->expiresAfter(900); |
|
73
|
|
|
} |
|
74
|
54 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Confirms if the cache item exists in the cache. |
|
78
|
|
|
* |
|
79
|
|
|
* Note: This method MAY avoid retrieving the cached value for performance |
|
80
|
|
|
* reasons, which could result in a race condition between exists() and get(). |
|
81
|
|
|
* To avoid that potential race condition use isHit() instead. |
|
82
|
|
|
* |
|
83
|
|
|
* @return boolean |
|
84
|
|
|
* |
|
85
|
|
|
* @since __DEPLOY_VERSION__ |
|
86
|
|
|
*/ |
|
87
|
|
|
public function exists() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->isHit(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the key for the current cache item. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string The key string for this cache item. |
|
96
|
|
|
* |
|
97
|
|
|
* @since 1.0 |
|
98
|
|
|
*/ |
|
99
|
39 |
|
public function getKey() |
|
100
|
|
|
{ |
|
101
|
39 |
|
return $this->key; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Retrieves the value of the item from the cache associated with this object's key. |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed The value corresponding to this cache item's key, or null if not found. |
|
108
|
|
|
* |
|
109
|
|
|
* @since 1.0 |
|
110
|
|
|
*/ |
|
111
|
41 |
|
public function get() |
|
112
|
|
|
{ |
|
113
|
41 |
|
return $this->value; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets the value represented by this cache item. |
|
118
|
|
|
* |
|
119
|
|
|
* If the value is set, we are assuming that there was a valid hit on the cache for the given key. |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $value The serializable value to be stored. |
|
122
|
|
|
* |
|
123
|
|
|
* @return $this |
|
124
|
|
|
* |
|
125
|
|
|
* @since 1.0 |
|
126
|
|
|
*/ |
|
127
|
49 |
|
public function set($value) |
|
128
|
|
|
{ |
|
129
|
49 |
|
$this->value = $value; |
|
130
|
49 |
|
$this->hit = true; |
|
131
|
|
|
|
|
132
|
49 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Confirms if the cache item lookup resulted in a cache hit. |
|
137
|
|
|
* |
|
138
|
|
|
* @return boolean True if the request resulted in a cache hit. False otherwise. |
|
139
|
|
|
* |
|
140
|
|
|
* @since 1.0 |
|
141
|
|
|
*/ |
|
142
|
33 |
|
public function isHit() |
|
143
|
|
|
{ |
|
144
|
33 |
|
return $this->hit; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Sets the expiration time for this cache item. |
|
149
|
|
|
* |
|
150
|
|
|
* @param \DateTimeInterface|null $expiration The point in time after which the item MUST be considered expired. |
|
151
|
|
|
* If null is passed explicitly, a default value MAY be used. If none is |
|
152
|
|
|
* set, the value should be stored permanently or for as long as the |
|
153
|
|
|
* implementation allows. |
|
154
|
|
|
* |
|
155
|
|
|
* @return $this |
|
156
|
|
|
* |
|
157
|
|
|
* @since __DEPLOY_VERSION__ |
|
158
|
|
|
*/ |
|
159
|
|
|
public function expiresAt($expiration) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->expiration = $expiration; |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Sets the expiration time for this cache item. |
|
168
|
|
|
* |
|
169
|
|
|
* @param int|\DateInterval|null $time The period of time from the present after which the item MUST be considered |
|
170
|
|
|
* expired. An integer parameter is understood to be the time in seconds until |
|
171
|
|
|
* expiration. |
|
172
|
|
|
* |
|
173
|
|
|
* @return $this |
|
174
|
|
|
* |
|
175
|
|
|
* @since __DEPLOY_VERSION__ |
|
176
|
|
|
*/ |
|
177
|
54 |
|
public function expiresAfter($time) |
|
178
|
|
|
{ |
|
179
|
54 |
|
if (is_integer($time)) |
|
180
|
|
|
{ |
|
181
|
54 |
|
$this->expiration = new \DateTime('now +' . $time . ' seconds'); |
|
182
|
|
|
} |
|
183
|
31 |
|
elseif ($time instanceof \DateInterval) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->expiration = new \DateTime('now'); |
|
186
|
|
|
$this->expiration->add($time); |
|
187
|
|
|
} |
|
188
|
|
|
else |
|
189
|
|
|
{ |
|
190
|
31 |
|
$this->expiration = new \DateTime('now + 900 seconds'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
54 |
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Returns the expiration time of a not-yet-expired cache item. |
|
198
|
|
|
* |
|
199
|
|
|
* If this cache item is a Cache Miss, this method MAY return the time at which the item expired or the current time if that is not available. |
|
200
|
|
|
* |
|
201
|
|
|
* @return \DateTimeInterface The timestamp at which this cache item will expire. |
|
202
|
|
|
* |
|
203
|
|
|
* @since __DEPLOY_VERSION__ |
|
204
|
|
|
*/ |
|
205
|
21 |
|
public function getExpiration(): \DateTimeInterface |
|
206
|
|
|
{ |
|
207
|
21 |
|
return $this->expiration; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|