Timothy J Warren
d1d1085d10
Some checks failed
Gitea - aviat/banker/master There was a failure building this commit
160 lines
2.8 KiB
PHP
160 lines
2.8 KiB
PHP
<?php declare(strict_types=1);
|
|
/**
|
|
* Banker
|
|
*
|
|
* A Caching library implementing psr/cache
|
|
*
|
|
* PHP version 7.1
|
|
*
|
|
* @package Banker
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
* @copyright 2016 - 2018 Timothy J. Warren
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
* @version 2.0.0
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
*/
|
|
namespace Aviat\Banker\Driver;
|
|
|
|
use function apcu_add;
|
|
use function apcu_clear_cache;
|
|
use function apcu_delete;
|
|
use function apcu_exists;
|
|
use function apcu_fetch;
|
|
use function apcu_store;
|
|
|
|
use Aviat\Banker\Exception\CacheException;
|
|
|
|
|
|
/**
|
|
* Memcached cache backend
|
|
*/
|
|
class ApcuDriver extends AbstractDriver {
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param array $config - Not used by this driver
|
|
* @param array $options - Not used by this driver
|
|
*/
|
|
public function __construct(array $config = [], array $options = [])
|
|
{
|
|
// noop
|
|
}
|
|
|
|
/**
|
|
* Destructor
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
// noop
|
|
}
|
|
|
|
/**
|
|
* See if a key currently exists in the cache
|
|
*
|
|
* @param string $key
|
|
* @return bool
|
|
*/
|
|
public function exists(string $key): bool
|
|
{
|
|
return apcu_exists($key) !== FALSE;
|
|
}
|
|
|
|
/**
|
|
* Get the value for the selected cache key
|
|
*
|
|
* @param string $key
|
|
* @return mixed
|
|
*/
|
|
public function get(string $key)
|
|
{
|
|
return apcu_fetch($key);
|
|
}
|
|
|
|
/**
|
|
* Retrieve a set of values by their cache key
|
|
*
|
|
* @param string[] $keys
|
|
* @return array
|
|
*/
|
|
public function getMultiple(array $keys = []): array
|
|
{
|
|
$status = FALSE;
|
|
return apcu_fetch($keys, $status);
|
|
}
|
|
|
|
/**
|
|
* Set a cached value
|
|
*
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @param int $expires
|
|
* @return DriverInterface
|
|
*/
|
|
public function set(string $key, $value, int $expires = 0): DriverInterface
|
|
{
|
|
if ( ! apcu_exists($key))
|
|
{
|
|
apcu_add($key, $value, $expires);
|
|
}
|
|
else
|
|
{
|
|
apcu_store($key, $value, $expires);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Remove an item from the cache
|
|
*
|
|
* @param string $key
|
|
* @return boolean
|
|
*/
|
|
public function delete(string $key): bool
|
|
{
|
|
return (bool) apcu_delete($key);
|
|
}
|
|
|
|
/**
|
|
* Remove multiple items from the cache
|
|
*
|
|
* @param string[] $keys
|
|
* @return boolean
|
|
*/
|
|
public function deleteMultiple(array $keys = []): bool
|
|
{
|
|
return (bool) apcu_delete($keys);
|
|
}
|
|
|
|
/**
|
|
* Empty the cache
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function flush(): bool
|
|
{
|
|
return apcu_clear_cache();
|
|
}
|
|
|
|
/**
|
|
* Set the specified key to expire at the given time
|
|
*
|
|
* @param string $key
|
|
* @param int $expires
|
|
* @return boolean
|
|
*/
|
|
public function expiresAt(string $key, int $expires): bool
|
|
{
|
|
if ($this->exists($key))
|
|
{
|
|
$value = $this->get($key);
|
|
return apcu_store($key, $value, $expires);
|
|
}
|
|
|
|
$this->getLogger()->log('warning', 'Tried to set expiration on a key that does not exist');
|
|
|
|
return FALSE;
|
|
}
|
|
}
|