Skip to content

Commit 0118201

Browse files
authored
Merge pull request #4629 from MGatner/cache-expires
Cache getMetadata() Format
2 parents 13fd337 + d7dcf0f commit 0118201

File tree

8 files changed

+63
-30
lines changed

8 files changed

+63
-30
lines changed

system/Cache/CacheInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ public function getCacheInfo();
108108
*
109109
* @param string $key Cache item name.
110110
*
111-
* @return mixed
111+
* @return array|false|null
112+
* Returns null if the item does not exist, otherwise array<string, mixed>
113+
* with at least the 'expires' key for absolute epoch expiry (or null).
114+
* Some handlers may return false when an item does not exist, which is deprecated.
112115
*/
113116
public function getMetaData(string $key);
114117

system/Cache/Handlers/FileHandler.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,36 +282,47 @@ public function getCacheInfo()
282282
*
283283
* @param string $key Cache item name.
284284
*
285-
* @return mixed
285+
* @return array|false|null
286+
* Returns null if the item does not exist, otherwise array<string, mixed>
287+
* with at least the 'expires' key for absolute epoch expiry (or null).
288+
* Some handlers may return false when an item does not exist, which is deprecated.
286289
*/
287290
public function getMetaData(string $key)
288291
{
289292
$key = $this->prefix . $key;
290293

291294
if (! is_file($this->path . $key))
292295
{
293-
return false;
296+
return false; // This will return null in a future release
294297
}
295298

296299
$data = @unserialize(file_get_contents($this->path . $key));
297300

298-
if (is_array($data))
301+
if (! is_array($data) || ! isset($data['ttl']))
299302
{
300-
$mtime = filemtime($this->path . $key);
303+
return false; // This will return null in a future release
304+
}
301305

302-
if (! isset($data['ttl']))
306+
// Consider expired items as missing
307+
$expire = $data['time'] + $data['ttl'];
308+
309+
// @phpstan-ignore-next-line
310+
if ($data['ttl'] > 0 && time() > $expire)
311+
{
312+
// If the file is still there then remove it
313+
if (is_file($this->path . $key))
303314
{
304-
return false;
315+
unlink($this->path . $key);
305316
}
306317

307-
return [
308-
'expire' => $mtime + $data['ttl'],
309-
'mtime' => $mtime,
310-
'data' => $data['data'],
311-
];
318+
return false; // This will return null in a future release
312319
}
313320

314-
return false;
321+
return [
322+
'expire' => $expire,
323+
'mtime' => filemtime($this->path . $key),
324+
'data' => $data['data'],
325+
];
315326
}
316327

317328
//--------------------------------------------------------------------

system/Cache/Handlers/MemcachedHandler.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ public function getCacheInfo()
342342
*
343343
* @param string $key Cache item name.
344344
*
345-
* @return mixed
345+
* @return array|false|null
346+
* Returns null if the item does not exist, otherwise array<string, mixed>
347+
* with at least the 'expires' key for absolute epoch expiry (or null).
348+
* Some handlers may return false when an item does not exist, which is deprecated.
346349
*/
347350
public function getMetaData(string $key)
348351
{
@@ -353,13 +356,16 @@ public function getMetaData(string $key)
353356
// if not an array, don't try to count for PHP7.2
354357
if (! is_array($stored) || count($stored) !== 3)
355358
{
356-
return false;
359+
return false; // This will return null in a future release
357360
}
358361

359-
list($data, $time, $ttl) = $stored;
362+
list($data, $time, $limit) = $stored;
363+
364+
// Calculate the remaining time to live from the original limit
365+
$ttl = time() - $time - $limit;
360366

361367
return [
362-
'expire' => $time + $ttl,
368+
'expire' => $limit > 0 ? $time + $limit : null,
363369
'mtime' => $time,
364370
'data' => $data,
365371
];

system/Cache/Handlers/PredisHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ public function getCacheInfo()
269269
*
270270
* @param string $key Cache item name.
271271
*
272-
* @return mixed
272+
* @return array|false|null
273+
* Returns null if the item does not exist, otherwise array<string, mixed>
274+
* with at least the 'expires' key for absolute epoch expiry (or null).
273275
*/
274276
public function getMetaData(string $key)
275277
{
@@ -278,8 +280,10 @@ public function getMetaData(string $key)
278280
if (isset($data['__ci_value']) && $data['__ci_value'] !== false)
279281
{
280282
$time = time();
283+
$ttl = $this->redis->ttl($key);
284+
281285
return [
282-
'expire' => $time + $this->redis->ttl($key),
286+
'expire' => $ttl > 0 ? time() + $ttl : null,
283287
'mtime' => $time,
284288
'data' => $data['__ci_value'],
285289
];

system/Cache/Handlers/RedisHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ public function getCacheInfo()
322322
*
323323
* @param string $key Cache item name.
324324
*
325-
* @return mixed
325+
* @return array|null
326+
* Returns null if the item does not exist, otherwise array<string, mixed>
327+
* with at least the 'expires' key for absolute epoch expiry (or null).
326328
*/
327329
public function getMetaData(string $key)
328330
{
@@ -333,8 +335,10 @@ public function getMetaData(string $key)
333335
if ($value !== null)
334336
{
335337
$time = time();
338+
$ttl = $this->redis->ttl($key);
339+
336340
return [
337-
'expire' => $time + $this->redis->ttl($key),
341+
'expire' => $ttl > 0 ? time() + $ttl : null,
338342
'mtime' => $time,
339343
'data' => $value,
340344
];

system/Cache/Handlers/WincacheHandler.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function get(string $key)
7171
$data = wincache_ucache_get($key, $success);
7272

7373
// Success returned by reference from wincache_ucache_get()
74-
return ($success) ? $data : null;
74+
return $success ? $data : null;
7575
}
7676

7777
//--------------------------------------------------------------------
@@ -147,7 +147,7 @@ public function increment(string $key, int $offset = 1)
147147
$success = false;
148148
$value = wincache_ucache_inc($key, $offset, $success);
149149

150-
return ($success === true) ? $value : false; // @phpstan-ignore-line
150+
return $success ? $value : false; // @phpstan-ignore-line
151151
}
152152

153153
//--------------------------------------------------------------------
@@ -169,7 +169,7 @@ public function decrement(string $key, int $offset = 1)
169169
$success = false;
170170
$value = wincache_ucache_dec($key, $offset, $success);
171171

172-
return ($success === true) ? $value : false; // @phpstan-ignore-line
172+
return $success ? $value : false; // @phpstan-ignore-line
173173
}
174174

175175
//--------------------------------------------------------------------
@@ -210,7 +210,10 @@ public function getCacheInfo()
210210
*
211211
* @param string $key Cache item name.
212212
*
213-
* @return mixed
213+
* @return array|false|null
214+
* Returns null if the item does not exist, otherwise array<string, mixed>
215+
* with at least the 'expires' key for absolute epoch expiry (or null).
216+
* Some handlers may return false when an item does not exist, which is deprecated.
214217
*
215218
* @codeCoverageIgnore
216219
*/
@@ -225,14 +228,14 @@ public function getMetaData(string $key)
225228
$hitcount = $stored['ucache_entries'][1]['hitcount'];
226229

227230
return [
228-
'expire' => $ttl - $age,
231+
'expire' => $ttl > 0 ? time() + $ttl : null,
229232
'hitcount' => $hitcount,
230233
'age' => $age,
231234
'ttl' => $ttl,
232235
];
233236
}
234237

235-
return false;
238+
return false; // This will return null in a future release
236239
}
237240

238241
//--------------------------------------------------------------------

user_guide_src/source/changelogs/v4.1.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Changes:
2626
- ``Entity::mutateDate`` uses external cast handler ``DatetimeCast::get``.
2727
- In order for ``Config\**`` classes to get their respective properties' values from the ``.env``, it is now necessary to namespace the property with the name of the class. Previously, the property names are enough but now disallowed because it can get system environment variables, like ``PATH``.
2828
- The array helper ``_array_search_dot`` is now marked for ``@internal`` use. As this is used by ``dot_array_search``, users should not use ``_array_search_dot`` directly in their code.
29+
- ``CacheInterface::getMetaData()`` returns ``null`` for misses, or an array with at least the "expires" key set to the absolute epoch expiration, or ``null`` for "never expires". The File, Memcached, and Wincache Handlers still return ``false`` which will become ``null`` in a future release.
2930

3031
Deprecations:
3132

user_guide_src/source/libraries/caching.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ Class Reference
215215
.. php:method:: getMetadata(string $key)
216216
217217
:param string $key: Cache item name
218-
:returns: Metadata for the cached item
219-
:rtype: mixed
218+
:returns: Metadata for the cached item. ``null`` for missing items, or an array with at least the "expire" key for absolute epoch expiry (``null`` for never expires).
219+
:rtype: array|null
220220

221221
This method will return detailed information on a specific item in the
222222
cache.
@@ -226,7 +226,8 @@ Class Reference
226226
var_dump($cache->getMetadata('my_cached_item'));
227227

228228
.. note:: The information returned and the structure of the data is dependent
229-
on which adapter is being used.
229+
on which adapter is being used. Some adapters (File, Memcached, Wincache)
230+
still return ``false`` for missing items.
230231

231232
*******
232233
Drivers

0 commit comments

Comments
 (0)