banker/src/AbstractTeller.php

137 lines
3.3 KiB
PHP

<?php declare(strict_types=1);
/**
* Banker
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
*
* PHP version 8+
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2016 - 2021 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 5.0.0
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker;
use DateInterval;
use Psr\Log\{LoggerInterface, LoggerAwareInterface};
/**
* Actual implementations for Simple Cache interface, so that TypeErrors can be caught and replaced with the
* PSR-specified exceptions
*/
abstract class AbstractTeller implements LoggerAwareInterface {
use _Driver;
use LoggerTrait;
/**
* Set up the cache backend
*
* @param array $config
* @param LoggerInterface|null $logger
*/
public function __construct(array $config, ?LoggerInterface $logger = NULL)
{
$this->driver = $this->loadDriver($config);
if ($logger !== NULL)
{
$this->setLogger($logger);
}
}
/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear(): bool
{
return $this->driver->flush();
}
/**
* Fetches a value from the cache.
* @throws Exception\InvalidArgumentException
*/
protected function get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
}
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
* @throws Exception\InvalidArgumentException
*/
protected function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->validateKey($key);
return $this->driver->set($key, $value, $ttl);
}
/**
* Delete an item from the cache by its unique key.
* @throws Exception\InvalidArgumentException
*/
protected function delete(string $key): bool
{
$this->validateKey($key);
return $this->driver->delete($key);
}
/**
* Obtains multiple cache items by their unique keys.
* @throws Exception\InvalidArgumentException
*/
protected function getMultiple(iterable $keys, mixed $default = null): iterable
{
// Check type of keys
if ( ! is_iterable($keys))
{
throw new Exception\InvalidArgumentException('Keys must be an array or a traversable object');
}
$this->validateKeys($keys);
return $this->driver->getMultiple((array)$keys);
}
/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*/
protected function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$this->validateKeys($values, TRUE);
return ($ttl === NULL)
? $this->driver->setMultiple((array)$values)
: $this->driver->setMultiple((array)$values, $ttl);
}
/**
* Deletes multiple cache items in a single operation.
*/
protected function deleteMultiple(iterable $keys): bool
{
$this->validateKeys($keys);
return $this->driver->deleteMultiple((array)$keys);
}
/**
* Determines whether an item is present in the cache.
* @throws Exception\InvalidArgumentException
*/
protected function has(string $key): bool
{
$this->validateKey($key);
return $this->driver->exists($key);
}
}