Skip to content

Commit

Permalink
Add getAge() method to Cache class
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Aug 29, 2019
1 parent 2818134 commit 8c68190
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions system/classes/Cache.php
Expand Up @@ -136,4 +136,15 @@ public static function exists($key)
{
return self::$isEnabled ? self::$instance->exists($key) : false;
}

/**
* Return the timestamp of cached item
*
* @param string $key
* @return int|false the timestamp when the item exists, false otherwise
*/
public static function getAge($key)
{
return self::$isEnabled ? self::$instance->getAge($key) : false;
}
}
16 changes: 15 additions & 1 deletion system/classes/Cache/APCu.php
Expand Up @@ -3,16 +3,19 @@
namespace Geeklog\Cache;

use Geeklog\CacheInterface;
use RuntimeException;

class APCu implements CacheInterface
{
/**
* APCu constructor.
*
* @throws RuntimeException
*/
public function __construct()
{
if (!is_callable('apcu_store')) {
throw new \RuntimeException('APCu extension is not loaded');
throw new RuntimeException('APCu extension is not loaded');
}
}

Expand Down Expand Up @@ -89,4 +92,15 @@ public function exists($key)
{
return apcu_exists($key);
}

/**
* Return the timestamp of cached item
*
* @param string $key
* @return int|false the timestamp when the item exists, false otherwise
*/
public function getAge($key)
{
return $this->exists($key) ? time() : false;
}
}
14 changes: 14 additions & 0 deletions system/classes/Cache/FileSystem.php
Expand Up @@ -209,4 +209,18 @@ public function exists($key)

return @is_file($fileName) && @is_readable($fileName);
}

/**
* Return the timestamp of cached item
*
* @param string $key
* @return int|false the timestamp when the item exists, false otherwise
*/
public function getAge($key)
{
$fileName = $this->getFileName($key);
clearstatcache();

return @is_file($fileName) && @is_readable($fileName) ? filemtime($fileName) : false;
}
}
8 changes: 8 additions & 0 deletions system/classes/CacheInterface.php
Expand Up @@ -58,4 +58,12 @@ public function delete($key);
* @return bool
*/
public function exists($key);

/**
* Return the timestamp of cached item
*
* @param string $key
* @return int|false the timestamp when the item exists, false otherwise
*/
public function getAge($key);
}

0 comments on commit 8c68190

Please sign in to comment.