1 | <?php |
||
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) |
|
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() |
||
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() |
|
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() |
|
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() |
|
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) |
||
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) |
|
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() |
|
209 | } |
||
210 |