Skip to content

Commit

Permalink
Improve expiry handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Apr 30, 2021
1 parent 8375fd5 commit c716acf
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion system/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function getCacheInfo();
*
* @return array|false|null
* Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expires' key for absolute epoch expiry.
* with at least the 'expires' key for absolute epoch expiry (or null).
* Some handlers may return false when an item does not exist, which is deprecated.
*/
public function getMetaData(string $key);
Expand Down
30 changes: 19 additions & 11 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public function getCacheInfo()
*
* @return array|false|null
* Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expires' key for absolute epoch expiry.
* with at least the 'expires' key for absolute epoch expiry (or null).
* Some handlers may return false when an item does not exist, which is deprecated.
*/
public function getMetaData(string $key)
Expand All @@ -298,23 +298,31 @@ public function getMetaData(string $key)

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

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

// Consider expired items as missing
$expire = $data['time'] + $data['ttl'];

if (! isset($data['ttl']))
// @phpstan-ignore-next-line
if ($data['ttl'] > 0 && time() > $expire)
{
// If the file is still there then remove it
if (is_file($this->path . $key))
{
return false; // This will return null in a future release
unlink($this->path . $key);
}

return [
'expire' => $data['time'] + $data['ttl'],
'mtime' => $mtime,
'data' => $data['data'],
];
return false; // This will return null in a future release
}

return false; // This will return null in a future release
return [
'expire' => $expire,
'mtime' => filemtime($this->path . $key),
'data' => $data['data'],
];
}

//--------------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions system/Cache/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function getCacheInfo()
*
* @return array|false|null
* Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expires' key for absolute epoch expiry.
* with at least the 'expires' key for absolute epoch expiry (or null).
* Some handlers may return false when an item does not exist, which is deprecated.
*/
public function getMetaData(string $key)
Expand All @@ -359,10 +359,13 @@ public function getMetaData(string $key)
return false; // This will return null in a future release
}

list($data, $time, $ttl) = $stored;
list($data, $time, $limit) = $stored;

// Calculate the remaining time to live from the original limit
$ttl = time() - $time - $limit;

return [
'expire' => $time + $ttl,
'expire' => $limit > 0 ? $time + $limit : null,
'mtime' => $time,
'data' => $data,
];
Expand Down
6 changes: 4 additions & 2 deletions system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function getCacheInfo()
*
* @return array|false|null
* Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expires' key for absolute epoch expiry.
* with at least the 'expires' key for absolute epoch expiry (or null).
*/
public function getMetaData(string $key)
{
Expand All @@ -282,8 +282,10 @@ public function getMetaData(string $key)
if (isset($data['__ci_value']) && $data['__ci_value'] !== false)
{
$time = time();
$ttl = $this->redis->ttl($key);

return [
'expire' => $time + $this->redis->ttl($key),
'expire' => $ttl > 0 ? time() + $ttl : null,
'mtime' => $time,
'data' => $data['__ci_value'],
];
Expand Down
6 changes: 4 additions & 2 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function getCacheInfo()
*
* @return array|null
* Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expires' key for absolute epoch expiry.
* with at least the 'expires' key for absolute epoch expiry (or null).
*/
public function getMetaData(string $key)
{
Expand All @@ -330,8 +330,10 @@ public function getMetaData(string $key)
if ($value !== null)
{
$time = time();
$ttl = $this->redis->ttl($key);

return [
'expire' => $time + $this->redis->ttl($key),
'expire' => $ttl > 0 ? time() + $ttl : null,
'mtime' => $time,
'data' => $value,
];
Expand Down
4 changes: 2 additions & 2 deletions system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function getCacheInfo()
*
* @return array|false|null
* Returns null if the item does not exist, otherwise array<string, mixed>
* with at least the 'expires' key for absolute epoch expiry.
* with at least the 'expires' key for absolute epoch expiry (or null).
* Some handlers may return false when an item does not exist, which is deprecated.
*
* @codeCoverageIgnore
Expand All @@ -228,7 +228,7 @@ public function getMetaData(string $key)
$hitcount = $stored['ucache_entries'][1]['hitcount'];

return [
'expire' => time() + $ttl,
'expire' => $ttl > 0 ? time() + $ttl : null,
'hitcount' => $hitcount,
'age' => $age,
'ttl' => $ttl,
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/caching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Class Reference
.. php:method:: getMetadata(string $key)
:param string $key: Cache item name
:returns: Metadata for the cached item with at least the "expires" key for absolute epoch expiry, ``null`` for missing items.
: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).
:rtype: array|null

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

0 comments on commit c716acf

Please sign in to comment.