2020-05-07 17:17:03 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Banker
|
|
|
|
*
|
2020-05-07 19:00:38 -04:00
|
|
|
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
|
2020-05-07 17:17:03 -04:00
|
|
|
*
|
2021-11-30 11:56:15 -05:00
|
|
|
* PHP version 8+
|
2020-05-07 17:17:03 -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
|
2020-05-07 17:17:03 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2021-11-30 11:56:15 -05:00
|
|
|
* @version 5.0.0
|
2020-05-07 17:17:03 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
|
|
*/
|
|
|
|
namespace Aviat\Banker;
|
|
|
|
|
2021-02-18 19:23:20 -05:00
|
|
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
2021-11-30 13:47:51 -05:00
|
|
|
use DateInterval;
|
2020-05-07 19:42:27 -04:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
2020-05-07 17:17:03 -04:00
|
|
|
use Psr\SimpleCache;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2021-11-30 13:47:51 -05:00
|
|
|
use TypeError;
|
2020-05-07 17:17:03 -04:00
|
|
|
|
2020-05-08 10:54:09 -04:00
|
|
|
/**
|
|
|
|
* Implements PSR-16 (SimpleCache)
|
|
|
|
*/
|
2021-11-30 13:47:51 -05:00
|
|
|
class Teller extends AbstractTeller implements SimpleCache\CacheInterface, LoggerAwareInterface {
|
2020-05-07 17:17:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a value from the cache.
|
|
|
|
*
|
|
|
|
* @param string $key The unique key of this item in the cache.
|
|
|
|
* @param mixed $default Default value to return if the key does not exist.
|
|
|
|
*
|
|
|
|
* @return mixed The value of the item from the cache, or $default in case of cache miss.
|
|
|
|
*
|
|
|
|
* @throws SimpleCache\InvalidArgumentException
|
|
|
|
* MUST be thrown if the $key string is not a legal value.
|
|
|
|
*/
|
2021-11-30 13:47:51 -05:00
|
|
|
public function get($key, $default = null): mixed
|
2020-05-07 17:17:03 -04:00
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return parent::get($key, $default);
|
|
|
|
}
|
|
|
|
catch (TypeError $e)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
|
|
|
|
*
|
|
|
|
* @param string $key The key of the item to store.
|
|
|
|
* @param mixed $value The value of the item to store, must be serializable.
|
2021-11-30 13:47:51 -05:00
|
|
|
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
|
2020-05-07 17:17:03 -04:00
|
|
|
* the driver supports TTL then the library may set a default value
|
|
|
|
* for it or let the driver take care of that.
|
|
|
|
*
|
|
|
|
* @return bool True on success and false on failure.
|
|
|
|
*
|
|
|
|
* @throws SimpleCache\InvalidArgumentException
|
|
|
|
* MUST be thrown if the $key string is not a legal value.
|
|
|
|
*/
|
2021-02-18 19:23:20 -05:00
|
|
|
public function set($key, mixed $value, $ttl = null): bool
|
2020-05-07 17:17:03 -04:00
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return parent::set($key, $value, $ttl);
|
|
|
|
}
|
|
|
|
catch (TypeError $e)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an item from the cache by its unique key.
|
|
|
|
*
|
|
|
|
* @param string $key The unique cache key of the item to delete.
|
|
|
|
*
|
|
|
|
* @return bool True if the item was successfully removed. False if there was an error.
|
|
|
|
*
|
|
|
|
* @throws SimpleCache\InvalidArgumentException
|
|
|
|
* MUST be thrown if the $key string is not a legal value.
|
|
|
|
*/
|
|
|
|
public function delete($key): bool
|
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return parent::delete($key);
|
|
|
|
}
|
|
|
|
catch (TypeError $e)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains multiple cache items by their unique keys.
|
|
|
|
*
|
|
|
|
* @param iterable $keys A list of keys that can obtained in a single operation.
|
|
|
|
* @param mixed $default Default value to return for keys that do not exist.
|
|
|
|
*
|
|
|
|
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
|
|
|
|
*
|
|
|
|
* @throws SimpleCache\InvalidArgumentException
|
|
|
|
* MUST be thrown if $keys is neither an array nor a Traversable,
|
|
|
|
* or if any of the $keys are not a legal value.
|
|
|
|
*/
|
2021-11-30 13:47:51 -05:00
|
|
|
public function getMultiple($keys, mixed $default = null): iterable
|
2020-05-07 17:17:03 -04:00
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
try
|
2021-02-18 19:23:20 -05:00
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
return parent::getMultiple($keys, $default);
|
|
|
|
}
|
|
|
|
catch (TypeError $e)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
2021-02-18 19:23:20 -05:00
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists a set of key => value pairs in the cache, with an optional TTL.
|
|
|
|
*
|
|
|
|
* @param iterable $values A list of key => value pairs for a multiple-set operation.
|
2021-11-30 13:47:51 -05:00
|
|
|
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
|
2020-05-07 17:17:03 -04:00
|
|
|
* the driver supports TTL then the library may set a default value
|
|
|
|
* for it or let the driver take care of that.
|
|
|
|
*
|
|
|
|
* @return bool True on success and false on failure.
|
|
|
|
*
|
|
|
|
* @throws SimpleCache\InvalidArgumentException
|
|
|
|
* MUST be thrown if $values is neither an array nor a Traversable,
|
|
|
|
* or if any of the $values are not a legal value.
|
|
|
|
*/
|
|
|
|
public function setMultiple($values, $ttl = null): bool
|
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
try
|
2021-02-18 19:23:20 -05:00
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
return parent::setMultiple($values, $ttl);
|
|
|
|
}
|
|
|
|
catch (TypeError $e)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
2021-02-18 19:23:20 -05:00
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes multiple cache items in a single operation.
|
|
|
|
*
|
|
|
|
* @param iterable $keys A list of string-based keys to be deleted.
|
|
|
|
*
|
|
|
|
* @return bool True if the items were successfully removed. False if there was an error.
|
|
|
|
*
|
|
|
|
* @throws SimpleCache\InvalidArgumentException
|
|
|
|
* MUST be thrown if $keys is neither an array nor a Traversable,
|
|
|
|
* or if any of the $keys are not a legal value.
|
|
|
|
*/
|
|
|
|
public function deleteMultiple($keys): bool
|
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
try
|
2021-02-18 19:23:20 -05:00
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
return parent::deleteMultiple($keys);
|
|
|
|
}
|
|
|
|
catch (TypeError $e)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
2021-02-18 19:23:20 -05:00
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether an item is present in the cache.
|
|
|
|
*
|
|
|
|
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
|
|
|
|
* and not to be used within your live applications operations for get/set, as this method
|
|
|
|
* is subject to a race condition where your has() will return true and immediately after,
|
|
|
|
* another script can remove it making the state of your app out of date.
|
|
|
|
*
|
|
|
|
* @param string $key The cache item key.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws SimpleCache\InvalidArgumentException
|
|
|
|
* MUST be thrown if the $key string is not a legal value.
|
|
|
|
*/
|
|
|
|
public function has($key): bool
|
|
|
|
{
|
2021-11-30 13:47:51 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return parent::has($key);
|
|
|
|
}
|
|
|
|
catch (TypeError $e)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
|
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
}
|
|
|
|
}
|