for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Soupmix\Cache;
use DateInterval;
use DateTime;
use Psr\SimpleCache\CacheInterface;
use function array_key_exists;
use function time;
class ArrayCache extends Common implements CacheInterface
{
private array $bucket = [];
public function get($key, $default = null)
$this->checkReservedCharacters($key);
if (! $this->has($key)) {
return $default;
}
return $this->bucket[$key][0];
public function set($key, $value, $ttl = null): bool
if ($ttl instanceof DateInterval) {
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time();
if ($ttl !== null) {
$ttl = ((int) $ttl) + time();
$this->bucket[$key] = [$value, $ttl ?? false];
return true;
public function delete($key): bool
if (array_key_exists($key, $this->bucket)) {
unset($this->bucket[$key]);
return false;
public function clear(): bool
$this->bucket = [];
public function has($key): bool
if (! array_key_exists($key, $this->bucket)) {
if ($this->bucket[$key][1] !== false && $this->bucket[$key][1] <= time()) {
$this->delete($key);