Skip to content

Commit c716acf

Browse files
committed
Improve expiry handling
1 parent 8375fd5 commit c716acf

File tree

7 files changed

+37
-22
lines changed

7 files changed

+37
-22
lines changed

system/Cache/CacheInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function getCacheInfo();
110110
*
111111
* @return array|false|null
112112
* Returns null if the item does not exist, otherwise array<string, mixed>
113-
* with at least the 'expires' key for absolute epoch expiry.
113+
* with at least the 'expires' key for absolute epoch expiry (or null).
114114
* Some handlers may return false when an item does not exist, which is deprecated.
115115
*/
116116
public function getMetaData(string $key);

system/Cache/Handlers/FileHandler.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public function getCacheInfo()
284284
*
285285
* @return array|false|null
286286
* Returns null if the item does not exist, otherwise array<string, mixed>
287-
* with at least the 'expires' key for absolute epoch expiry.
287+
* with at least the 'expires' key for absolute epoch expiry (or null).
288288
* Some handlers may return false when an item does not exist, which is deprecated.
289289
*/
290290
public function getMetaData(string $key)
@@ -298,23 +298,31 @@ public function getMetaData(string $key)
298298

299299
$data = @unserialize(file_get_contents($this->path . $key));
300300

301-
if (is_array($data))
301+
if (! is_array($data) || ! isset($data['ttl']))
302302
{
303-
$mtime = filemtime($this->path . $key);
303+
return false; // This will return null in a future release
304+
}
305+
306+
// Consider expired items as missing
307+
$expire = $data['time'] + $data['ttl'];
304308

305-
if (! isset($data['ttl']))
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))
306314
{
307-
return false; // This will return null in a future release
315+
unlink($this->path . $key);
308316
}
309317

310-
return [
311-
'expire' => $data['time'] + $data['ttl'],
312-
'mtime' => $mtime,
313-
'data' => $data['data'],
314-
];
318+
return false; // This will return null in a future release
315319
}
316320

317-
return false; // This will return null in a future release
321+
return [
322+
'expire' => $expire,
323+
'mtime' => filemtime($this->path . $key),
324+
'data' => $data['data'],
325+
];
318326
}
319327

320328
//--------------------------------------------------------------------

system/Cache/Handlers/MemcachedHandler.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function getCacheInfo()
344344
*
345345
* @return array|false|null
346346
* Returns null if the item does not exist, otherwise array<string, mixed>
347-
* with at least the 'expires' key for absolute epoch expiry.
347+
* with at least the 'expires' key for absolute epoch expiry (or null).
348348
* Some handlers may return false when an item does not exist, which is deprecated.
349349
*/
350350
public function getMetaData(string $key)
@@ -359,10 +359,13 @@ public function getMetaData(string $key)
359359
return false; // This will return null in a future release
360360
}
361361

362-
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;
363366

364367
return [
365-
'expire' => $time + $ttl,
368+
'expire' => $limit > 0 ? $time + $limit : null,
366369
'mtime' => $time,
367370
'data' => $data,
368371
];

system/Cache/Handlers/PredisHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function getCacheInfo()
273273
*
274274
* @return array|false|null
275275
* Returns null if the item does not exist, otherwise array<string, mixed>
276-
* with at least the 'expires' key for absolute epoch expiry.
276+
* with at least the 'expires' key for absolute epoch expiry (or null).
277277
*/
278278
public function getMetaData(string $key)
279279
{
@@ -282,8 +282,10 @@ public function getMetaData(string $key)
282282
if (isset($data['__ci_value']) && $data['__ci_value'] !== false)
283283
{
284284
$time = time();
285+
$ttl = $this->redis->ttl($key);
286+
285287
return [
286-
'expire' => $time + $this->redis->ttl($key),
288+
'expire' => $ttl > 0 ? time() + $ttl : null,
287289
'mtime' => $time,
288290
'data' => $data['__ci_value'],
289291
];

system/Cache/Handlers/RedisHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public function getCacheInfo()
319319
*
320320
* @return array|null
321321
* Returns null if the item does not exist, otherwise array<string, mixed>
322-
* with at least the 'expires' key for absolute epoch expiry.
322+
* with at least the 'expires' key for absolute epoch expiry (or null).
323323
*/
324324
public function getMetaData(string $key)
325325
{
@@ -330,8 +330,10 @@ public function getMetaData(string $key)
330330
if ($value !== null)
331331
{
332332
$time = time();
333+
$ttl = $this->redis->ttl($key);
334+
333335
return [
334-
'expire' => $time + $this->redis->ttl($key),
336+
'expire' => $ttl > 0 ? time() + $ttl : null,
335337
'mtime' => $time,
336338
'data' => $value,
337339
];

system/Cache/Handlers/WincacheHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function getCacheInfo()
212212
*
213213
* @return array|false|null
214214
* Returns null if the item does not exist, otherwise array<string, mixed>
215-
* with at least the 'expires' key for absolute epoch expiry.
215+
* with at least the 'expires' key for absolute epoch expiry (or null).
216216
* Some handlers may return false when an item does not exist, which is deprecated.
217217
*
218218
* @codeCoverageIgnore
@@ -228,7 +228,7 @@ public function getMetaData(string $key)
228228
$hitcount = $stored['ucache_entries'][1]['hitcount'];
229229

230230
return [
231-
'expire' => time() + $ttl,
231+
'expire' => $ttl > 0 ? time() + $ttl : null,
232232
'hitcount' => $hitcount,
233233
'age' => $age,
234234
'ttl' => $ttl,

user_guide_src/source/libraries/caching.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Class Reference
216216
.. php:method:: getMetadata(string $key)
217217
218218
:param string $key: Cache item name
219-
:returns: Metadata for the cached item with at least the "expires" key for absolute epoch expiry, ``null`` for missing items.
219+
: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).
220220
:rtype: array|null
221221

222222
This method will return detailed information on a specific item in the

0 commit comments

Comments
 (0)