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 \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
|
77 |
|
public function __construct($key, $ttl = null) |
59
|
|
|
{ |
60
|
77 |
|
$this->key = $key; |
61
|
|
|
|
62
|
77 |
|
if (is_int($ttl)) |
63
|
77 |
|
{ |
64
|
|
|
$this->expiresAfter($ttl); |
65
|
|
|
} |
66
|
77 |
|
elseif ($ttl instanceof \DateTimeInterface) |
67
|
|
|
{ |
68
|
|
|
$this->expiresAt($ttl); |
69
|
|
|
} |
70
|
|
|
else |
71
|
|
|
{ |
72
|
77 |
|
$this->expiresAfter(900); |
73
|
|
|
} |
74
|
77 |
|
} |
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
|
57 |
|
public function getKey() |
100
|
|
|
{ |
101
|
57 |
|
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
|
61 |
|
public function get() |
112
|
|
|
{ |
113
|
61 |
|
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
|
72 |
|
public function set($value) |
128
|
|
|
{ |
129
|
72 |
|
$this->value = $value; |
130
|
72 |
|
$this->hit = true; |
131
|
|
|
|
132
|
72 |
|
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
|
48 |
|
public function isHit() |
143
|
|
|
{ |
144
|
48 |
|
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
|
77 |
|
public function expiresAfter($time) |
178
|
|
|
{ |
179
|
77 |
|
if (is_integer($time)) |
180
|
77 |
|
{ |
181
|
77 |
|
$this->expiration = new \DateTime('now +' . $time . ' seconds'); |
182
|
77 |
|
} |
183
|
45 |
|
elseif ($time instanceof \DateInterval) |
184
|
|
|
{ |
185
|
|
|
$this->expiration = new \DateTime('now'); |
186
|
|
|
$this->expiration->add($time); |
187
|
|
|
} |
188
|
|
|
else |
189
|
|
|
{ |
190
|
45 |
|
$this->expiration = new \DateTime('now + 900 seconds'); |
191
|
|
|
} |
192
|
|
|
|
193
|
77 |
|
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
|
35 |
|
public function getExpiration() |
206
|
|
|
{ |
207
|
35 |
|
return $this->expiration; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|