diff --git a/system/Cache/CacheInterface.php b/system/Cache/CacheInterface.php index 65840af188a2..8a929fa33157 100644 --- a/system/Cache/CacheInterface.php +++ b/system/Cache/CacheInterface.php @@ -110,7 +110,7 @@ public function getCacheInfo(); * * @return array|false|null * Returns null if the item does not exist, otherwise array - * with at least the 'expires' key for absolute epoch expiry (or null). + * with at least the 'expire' 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); diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index 8fe290a52714..8f492590939d 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -278,7 +278,7 @@ public function getCacheInfo() * * @return array|false|null * Returns null if the item does not exist, otherwise array - * with at least the 'expires' key for absolute epoch expiry (or null). + * with at least the 'expire' 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) diff --git a/system/Cache/Handlers/MemcachedHandler.php b/system/Cache/Handlers/MemcachedHandler.php index 00cfcb203c65..7452330a83c4 100644 --- a/system/Cache/Handlers/MemcachedHandler.php +++ b/system/Cache/Handlers/MemcachedHandler.php @@ -337,7 +337,7 @@ public function getCacheInfo() * * @return array|false|null * Returns null if the item does not exist, otherwise array - * with at least the 'expires' key for absolute epoch expiry (or null). + * with at least the 'expire' 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) diff --git a/system/Cache/Handlers/PredisHandler.php b/system/Cache/Handlers/PredisHandler.php index 3a89173c0c73..1892e812f366 100644 --- a/system/Cache/Handlers/PredisHandler.php +++ b/system/Cache/Handlers/PredisHandler.php @@ -274,7 +274,7 @@ public function getCacheInfo() * * @return array|false|null * Returns null if the item does not exist, otherwise array - * with at least the 'expires' key for absolute epoch expiry (or null). + * with at least the 'expire' key for absolute epoch expiry (or null). */ public function getMetaData(string $key) { diff --git a/system/Cache/Handlers/RedisHandler.php b/system/Cache/Handlers/RedisHandler.php index 58fcec83d864..96e9a850f33f 100644 --- a/system/Cache/Handlers/RedisHandler.php +++ b/system/Cache/Handlers/RedisHandler.php @@ -316,7 +316,7 @@ public function getCacheInfo() * * @return array|null * Returns null if the item does not exist, otherwise array - * with at least the 'expires' key for absolute epoch expiry (or null). + * with at least the 'expire' key for absolute epoch expiry (or null). */ public function getMetaData(string $key) { diff --git a/system/Cache/Handlers/WincacheHandler.php b/system/Cache/Handlers/WincacheHandler.php index 5f48e7016eb0..daf041a6d04c 100644 --- a/system/Cache/Handlers/WincacheHandler.php +++ b/system/Cache/Handlers/WincacheHandler.php @@ -178,7 +178,7 @@ public function getCacheInfo() * * @return array|false|null * Returns null if the item does not exist, otherwise array - * with at least the 'expires' key for absolute epoch expiry (or null). + * with at least the 'expire' 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) diff --git a/system/Test/Mock/MockCache.php b/system/Test/Mock/MockCache.php index 368b3b0f7ee9..f82d2c2be252 100644 --- a/system/Test/Mock/MockCache.php +++ b/system/Test/Mock/MockCache.php @@ -12,23 +12,24 @@ namespace CodeIgniter\Test\Mock; use CodeIgniter\Cache\CacheInterface; +use CodeIgniter\Cache\Handlers\BaseHandler; use Closure; -class MockCache implements CacheInterface +class MockCache extends BaseHandler implements CacheInterface { /** - * Prefixed to all cache names. + * Mock cache storage. * - * @var string + * @var array */ - protected $prefix; + protected $cache = []; /** - * Mock cache storage. + * Expiration times. * - * @var array + * @var ?int[] */ - protected $cache = []; + protected $expirations = []; //-------------------------------------------------------------------- @@ -37,7 +38,6 @@ class MockCache implements CacheInterface */ public function initialize() { - // Not to see here... } //-------------------------------------------------------------------- @@ -51,7 +51,7 @@ public function initialize() */ public function get(string $key) { - $key = $this->prefix . $key; + $key = static::validateKey($key, $this->prefix); return array_key_exists($key, $this->cache) ? $this->cache[$key] @@ -96,13 +96,14 @@ public function remember(string $key, int $ttl, Closure $callback) * @param integer $ttl Time To Live, in seconds (default 60) * @param boolean $raw Whether to store the raw value. * - * @return mixed + * @return boolean */ public function save(string $key, $value, int $ttl = 60, bool $raw = false) { - $key = $this->prefix . $key; + $key = static::validateKey($key, $this->prefix); - $this->cache[$key] = $value; + $this->cache[$key] = $value; + $this->expirations[$key] = $ttl > 0 ? time() + $ttl : null; return true; } @@ -114,11 +115,21 @@ public function save(string $key, $value, int $ttl = 60, bool $raw = false) * * @param string $key Cache item name * - * @return mixed + * @return boolean */ public function delete(string $key) { + $key = static::validateKey($key, $this->prefix); + + if (! isset($this->cache[$key])) + { + return false; + } + unset($this->cache[$key]); + unset($this->expirations[$key]); + + return true; } //-------------------------------------------------------------------- @@ -128,19 +139,22 @@ public function delete(string $key) * * @param string $pattern Cache items glob-style pattern * - * @return boolean + * @return integer */ public function deleteMatching(string $pattern) { + $count = 0; foreach (array_keys($this->cache) as $key) { if (fnmatch($pattern, $key)) { + $count++; unset($this->cache[$key]); + unset($this->expirations[$key]); } } - return true; + return $count; } //-------------------------------------------------------------------- @@ -151,12 +165,11 @@ public function deleteMatching(string $pattern) * @param string $key Cache ID * @param integer $offset Step/value to increase by * - * @return mixed + * @return boolean */ public function increment(string $key, int $offset = 1) { - $key = $this->prefix . $key; - + $key = static::validateKey($key, $this->prefix); $data = $this->cache[$key] ?: null; if (empty($data)) @@ -179,11 +192,11 @@ public function increment(string $key, int $offset = 1) * @param string $key Cache ID * @param integer $offset Step/value to increase by * - * @return mixed + * @return boolean */ public function decrement(string $key, int $offset = 1) { - $key = $this->prefix . $key; + $key = static::validateKey($key, $this->prefix); $data = $this->cache[$key] ?: null; @@ -204,11 +217,14 @@ public function decrement(string $key, int $offset = 1) /** * Will delete all items in the entire cache. * - * @return mixed + * @return boolean */ public function clean() { - $this->cache = []; + $this->cache = []; + $this->expirations = []; + + return true; } //-------------------------------------------------------------------- @@ -233,11 +249,27 @@ public function getCacheInfo() * * @param string $key Cache item name. * - * @return mixed + * @return array|null + * Returns null if the item does not exist, otherwise array + * with at least the 'expire' key for absolute epoch expiry (or null). */ public function getMetaData(string $key) { - return false; + // Misses return null + if (! array_key_exists($key, $this->expirations)) + { + return null; + } + + // Count expired items as a miss + if (is_int($this->expirations[$key]) && $this->expirations[$key] > time()) + { + return null; + } + + return [ + 'expire' => $this->expirations[$key], + ]; } //--------------------------------------------------------------------