for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace IQParts\Cache\Adapter;
final class MemoryAdapter implements CacheAdapterInterface
{
/**
* @var array
*/
private $data = [];
private $timeToLive = [];
* @param string $key
* @return mixed
public function get(string $key)
if (!isset($this->data[$key])) {
return null;
}
if (!isset($this->timeToLive[$key])) {
return $this->data[$key];
if ($this->ttl($key) > 0) {
unset($this->timeToLive[$key], $this->data[$key]);
* @param $value
* @param int|null $ttl
* @return void
public function set(string $key, $value, int $ttl = null): void
$this->data[$key] = $value;
if ($ttl !== null) {
$this->timeToLive[$key] = time() + $ttl;
public function delete(string $key): void
unset($this->data[$key], $this->timeToLive[$key]);
public function keys($key = '*')
$matches = [];
foreach ($this->data as $name => $value) {
if (fnmatch($key, $name)) {
$matches[] = $name;
return $matches;
* @param $key
* @return int
public function ttl($key): int
if (isset($this->timeToLive[$key])) {
return max($this->timeToLive[$key] - time(), 0);
return self::NO_TTL;