Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (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);
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,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 (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)
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,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 (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)
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,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 (or null).
* with at least the 'expire' key for absolute epoch expiry (or null).
*/
public function getMetaData(string $key)
{
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,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 (or null).
* with at least the 'expire' key for absolute epoch expiry (or null).
*/
public function getMetaData(string $key)
{
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,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 (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)
Expand Down
80 changes: 56 additions & 24 deletions system/Test/Mock/MockCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

//--------------------------------------------------------------------

Expand All @@ -37,7 +38,6 @@ class MockCache implements CacheInterface
*/
public function initialize()
{
// Not to see here...
}

//--------------------------------------------------------------------
Expand All @@ -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]
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

//--------------------------------------------------------------------
Expand All @@ -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;
}

//--------------------------------------------------------------------
Expand All @@ -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))
Expand All @@ -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;

Expand All @@ -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;
}

//--------------------------------------------------------------------
Expand All @@ -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<string, mixed>
* 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],
];
}

//--------------------------------------------------------------------
Expand Down