2017-01-17 11:21:07 -05: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)
|
2017-01-17 11:21:07 -05:00
|
|
|
*
|
2020-05-07 19:00:38 -04:00
|
|
|
* PHP version 7.4
|
2017-01-17 11:21:07 -05:00
|
|
|
*
|
|
|
|
* @package Banker
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-05-07 19:00:38 -04:00
|
|
|
* @copyright 2016 - 2020 Timothy J. Warren
|
2017-01-17 11:21:07 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-05-08 12:30:43 -04:00
|
|
|
* @version 3.1.0
|
2017-01-17 11:21:07 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
|
|
*/
|
|
|
|
namespace Aviat\Banker\Driver;
|
|
|
|
|
|
|
|
use Aviat\Banker\Exception\CacheException;
|
|
|
|
|
2020-05-07 17:17:03 -04:00
|
|
|
use function apcu_clear_cache;
|
|
|
|
use function apcu_delete;
|
|
|
|
use function apcu_exists;
|
|
|
|
use function apcu_fetch;
|
|
|
|
use function apcu_store;
|
2017-01-17 11:21:07 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Memcached cache backend
|
|
|
|
*/
|
|
|
|
class ApcuDriver extends AbstractDriver {
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2017-01-17 11:28:13 -05:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param array $config - Not used by this driver
|
|
|
|
* @param array $options - Not used by this driver
|
2019-12-10 11:00:46 -05:00
|
|
|
* @throws CacheException
|
2020-05-07 17:17:03 -04:00
|
|
|
* @codeCoverageIgnore
|
2017-01-17 11:28:13 -05:00
|
|
|
*/
|
|
|
|
public function __construct(array $config = [], array $options = [])
|
|
|
|
{
|
2019-12-10 11:00:46 -05:00
|
|
|
if ( ! extension_loaded('apcu'))
|
|
|
|
{
|
|
|
|
throw new CacheException('This driver requires the APCU extension');
|
|
|
|
}
|
2017-01-17 11:28:13 -05:00
|
|
|
}
|
2018-11-15 16:37:50 -05:00
|
|
|
|
2017-01-17 11:21:07 -05:00
|
|
|
/**
|
|
|
|
* See if a key currently exists in the cache
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists(string $key): bool
|
|
|
|
{
|
2020-05-07 17:17:03 -04:00
|
|
|
return apcu_exists($key) !== FALSE;
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value for the selected cache key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get(string $key)
|
|
|
|
{
|
2020-05-07 17:17:03 -04:00
|
|
|
return apcu_fetch($key);
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a set of values by their cache key
|
|
|
|
*
|
|
|
|
* @param string[] $keys
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMultiple(array $keys = []): array
|
|
|
|
{
|
2017-01-17 11:52:51 -05:00
|
|
|
$status = FALSE;
|
2020-05-07 17:17:03 -04:00
|
|
|
return apcu_fetch($keys, $status);
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a cached value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @param int $expires
|
2020-05-07 17:17:03 -04:00
|
|
|
* @return bool
|
2017-01-17 11:21:07 -05:00
|
|
|
*/
|
2020-05-07 19:42:27 -04:00
|
|
|
public function set(string $key, $value, ?int $expires = 0): bool
|
2017-01-17 11:21:07 -05:00
|
|
|
{
|
2020-05-07 17:17:03 -04:00
|
|
|
$ttl = $this->getTTLFromExpiration($expires);
|
2017-01-17 11:21:07 -05:00
|
|
|
|
2020-05-07 17:17:03 -04:00
|
|
|
return apcu_store($key, $value, $ttl);
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the cache
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function delete(string $key): bool
|
|
|
|
{
|
2020-05-07 17:17:03 -04:00
|
|
|
return apcu_delete($key);
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove multiple items from the cache
|
|
|
|
*
|
|
|
|
* @param string[] $keys
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function deleteMultiple(array $keys = []): bool
|
|
|
|
{
|
2020-05-07 17:17:03 -04:00
|
|
|
$deleted = apcu_delete($keys);
|
|
|
|
return ($keys <=> $deleted) === 0;
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty the cache
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function flush(): bool
|
|
|
|
{
|
2020-05-07 17:17:03 -04:00
|
|
|
return apcu_clear_cache();
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2020-05-07 17:17:03 -04:00
|
|
|
$ttl = $this->getTTLFromExpiration($expires);
|
|
|
|
return apcu_store($key, $value, $ttl);
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->getLogger()->log('warning', 'Tried to set expiration on a key that does not exist');
|
2017-01-17 11:21:07 -05:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-05-07 17:17:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert expiration date argument into TTL argument
|
|
|
|
*
|
|
|
|
* @param int $expires
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getTTLFromExpiration(int $expires): int
|
|
|
|
{
|
|
|
|
$ttl = $expires - time();
|
|
|
|
|
|
|
|
return ($ttl < 0) ? 0 : $ttl;
|
|
|
|
}
|
2017-01-17 11:21:07 -05:00
|
|
|
}
|