banker/src/Driver/NullDriver.php

140 lines
2.5 KiB
PHP
Raw Normal View History

2016-10-19 09:57:06 -04:00
<?php declare(strict_types=1);
2016-09-05 16:43:37 -04:00
/**
* Banker
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
2016-09-05 16:43:37 -04:00
*
2021-11-30 11:56:15 -05:00
* PHP version 8+
2016-09-05 16:43:37 -04:00
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
2021-02-05 17:05:56 -05:00
* @copyright 2016 - 2021 Timothy J. Warren
2016-09-05 16:43:37 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2021-12-02 15:30:20 -05:00
* @version 4.0.0
2016-09-05 16:43:37 -04:00
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker\Driver;
use DateInterval;
2016-09-05 16:43:37 -04:00
/**
* Cache backend for use without a cache server. Only does transient
* in-memory caching
*/
2016-09-06 17:03:43 -04:00
class NullDriver extends AbstractDriver {
2016-09-05 16:43:37 -04:00
/**
* In memory store
*
* @var array
*/
2020-05-07 17:17:03 -04:00
protected array $store = [];
2016-09-05 16:43:37 -04:00
/**
* NullDriver constructor
2016-09-05 16:43:37 -04:00
*/
public function __construct()
2016-09-05 16:43:37 -04:00
{
$this->store = [];
}
2016-09-06 20:57:24 -04:00
2016-09-05 16:43:37 -04:00
/**
* See if a key currently exists in the cache
*
* @param string $key
* @return bool
*/
2016-10-19 09:57:06 -04:00
public function exists(string $key): bool
2016-09-05 16:43:37 -04:00
{
2016-10-19 09:57:06 -04:00
return array_key_exists($key, $this->store);
2016-09-05 16:43:37 -04:00
}
/**
* Get the value for the selected cache key
*
* @param string $key
* @return mixed
*/
public function get(string $key): mixed
2016-09-05 16:43:37 -04:00
{
return $this->exists($key)
2016-09-06 20:57:24 -04:00
? $this->store[$key]
2016-09-06 17:03:43 -04:00
: NULL;
2016-09-05 16:43:37 -04:00
}
/**
* Set a cached value
*
* @param string $key
* @param mixed $value
* @param DateInterval|int|null $expires
2020-05-07 17:17:03 -04:00
* @return bool
2016-09-05 16:43:37 -04:00
*/
public function set(string $key, mixed $value, DateInterval|int|null $expires = NULL): bool
2016-09-05 16:43:37 -04:00
{
$this->store[$key] = $value;
2020-05-07 17:17:03 -04:00
return $this->store[$key] === $value;
2016-09-05 16:43:37 -04:00
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
2016-10-19 09:57:06 -04:00
public function delete(string $key): bool
2016-09-05 16:43:37 -04:00
{
2020-05-08 18:58:25 -04:00
// Don't return true if the key didn't exist to begin with
if ( ! array_key_exists($key, $this->store))
{
return FALSE;
}
2016-09-05 16:43:37 -04:00
unset($this->store[$key]);
return ( ! array_key_exists($key, $this->store));
}
/**
* Remove multiple items from the cache
*
* @param string[] $keys
* @return boolean
*/
2016-10-19 09:57:06 -04:00
public function deleteMultiple(array $keys = []): bool
2016-09-05 16:43:37 -04:00
{
$this->validateKeys($keys);
2016-09-05 16:43:37 -04:00
$res = TRUE;
foreach($keys as $key)
{
$res = $res && $this->delete($key);
}
return $res;
}
/**
* Empty the cache
*
* @return boolean
*/
2016-10-19 09:57:06 -04:00
public function flush(): bool
2016-09-05 16:43:37 -04:00
{
$this->store = [];
return TRUE;
}
2016-09-06 20:57:24 -04:00
2016-09-06 17:03:43 -04:00
/**
* Set the specified key to expire at the given time
*
* @param string $key
* @param int $expires
* @return boolean
*/
2016-10-19 09:57:06 -04:00
public function expiresAt(string $key, int $expires): bool
2016-09-06 17:03:43 -04:00
{
//noop
return array_key_exists($key, $this->store);
2016-09-06 17:03:43 -04:00
}
2016-09-05 16:43:37 -04:00
}