Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
66.67% |
6 / 9 |
CRAP | |
82.22% |
37 / 45 |
CacheProvider | |
0.00% |
0 / 1 |
|
66.67% |
6 / 9 |
31.10 | |
82.22% |
37 / 45 |
get | |
100.00% |
1 / 1 |
3 | |
100.00% |
7 / 7 |
|||
set | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
delete | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
clear | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
has | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
getMultiple | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
setMultiple | |
0.00% |
0 / 1 |
3.07 | |
80.00% |
4 / 5 |
|||
deleteMultiple | |
0.00% |
0 / 1 |
3.07 | |
80.00% |
4 / 5 |
|||
isExpired | |
0.00% |
0 / 1 |
10.50 | |
50.00% |
6 / 12 |
|||
doGet | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
doSet | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
doDelete | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
doClear | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||||
doHas | n/a |
0 / 0 |
1 | n/a |
0 / 0 |
<?php | |
/* | |
* This file is part of the Shieldon Simple Cache package. | |
* | |
* (c) Terry L. <contact@terryl.in> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
declare(strict_types=1); | |
namespace Shieldon\SimpleCache; | |
use Psr\SimpleCache\CacheInterface; | |
use DateInterval; | |
use Datetime; | |
/** | |
* The abstract class for cache service providers. | |
*/ | |
abstract class CacheProvider implements CacheInterface | |
{ | |
use AssertTrait; | |
/** | |
* @inheritDoc | |
*/ | |
public function get($key, $default = null) | |
{ | |
$data = $this->doGet($key); | |
if (!empty($data)) { | |
if ($this->isExpired($data['ttl'], $data['timestamp'])) { | |
$this->delete($key); | |
$data['value'] = $default; | |
} | |
$default = $data['value']; | |
} | |
return $default; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function set($key, $value, $ttl = null) | |
{ | |
$this->assertArgumentString($key); | |
$this->assertValidTypeOfTtl($ttl); | |
$timestamp = time(); | |
return $this->doSet($key, $value, $ttl, $timestamp); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function delete($key) | |
{ | |
$this->assertArgumentString($key); | |
return $this->doDelete($key); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function clear() | |
{ | |
return $this->doClear(); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function has($key) | |
{ | |
$this->assertArgumentString($key); | |
if ($this->doHas($key)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function getMultiple($keys, $default = null) | |
{ | |
$this->assertArgumentIterable($keys); | |
$data = []; | |
foreach ($keys as $key) { | |
$data[$key] = $this->get($key, $default); | |
} | |
return $data; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function setMultiple($values, $ttl = null) | |
{ | |
$this->assertArgumentIterable($values); | |
foreach ($values as $key => $value) { | |
if (!$this->set($key, $value, $ttl)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function deleteMultiple($keys) | |
{ | |
$this->assertArgumentIterable($keys); | |
foreach ($keys as $key) { | |
if (!$this->doDelete($key)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Check if the TTL is expired or not. | |
* | |
* @param int|null|DateInterval $ttl The time to live of a cached data. | |
* @param int $timestamp The unix timesamp that want to check. | |
* | |
* @return bool | |
*/ | |
protected function isExpired($ttl, int $timestamp): bool | |
{ | |
$now = time(); | |
if (is_null($ttl)) { | |
return false; | |
} elseif (is_integer($ttl) && ($now - $timestamp < $ttl)) { | |
return false; | |
} elseif ($ttl instanceof DateInterval) { | |
$datetimeObj = new DateTime(); | |
$datetimeObj->add($ttl); | |
$datetimeObj->getTimestamp(); | |
if ($now - $timestamp < $datetimeObj->getTimestamp()) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Fetch a cache by an extended Cache Driver. | |
* | |
* @param string $key The key of a cache. | |
* @param mixed $default Default value to return if the key does not exist. | |
* | |
* @return array The data structure looks like: | |
* | |
* [ | |
* [ | |
* 'value' => (mixed) $value | |
* 'ttl' => (int) $ttl, | |
* 'timestamp' => (int) $timestamp, | |
* ], | |
* ... | |
* ] | |
* | |
*/ | |
abstract protected function doGet(string $key): array; | |
/** | |
* Set a cache by an extended Cache Driver. | |
* | |
* @param string $key The key of a cache. | |
* @param mixed $value The value of a cache. (serialized) | |
* @param int $ttl The time to live for a cache. | |
* @param int $timestamp The time to store a cache. | |
* | |
* @return bool | |
*/ | |
abstract protected function doSet(string $key, $value, int $ttl, int $timestamp): bool; | |
/** | |
* Delete a cache by an extended Cache Driver. | |
* | |
* @param string $key The key of a cache. | |
* | |
* @return bool | |
*/ | |
abstract protected function doDelete(string $key): bool; | |
/** | |
* Delete all caches by an extended Cache Driver. | |
* | |
* @return bool | |
*/ | |
abstract protected function doClear(): bool; | |
/** | |
* Check if a cahce exists or not. | |
* | |
* @param string $key The key of a cache. | |
* | |
* @return bool | |
*/ | |
abstract protected function doHas(string $key): bool; | |
} |