banker/src/Driver/ApcuDriver.php

189 lines
3.6 KiB
PHP
Raw Normal View History

2017-01-17 11:21:07 -05:00
<?php declare(strict_types=1);
/**
* Banker
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
2017-01-17 11:21:07 -05:00
*
2021-02-19 11:47:20 -05:00
* PHP version 8
2017-01-17 11:21:07 -05:00
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
2021-02-05 17:05:56 -05:00
* @copyright 2016 - 2021 Timothy J. Warren
2017-01-17 11:21:07 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2021-02-19 11:47:20 -05:00
* @version 4.0.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;
use DateInterval;
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 {
2017-01-17 11:28:13 -05:00
/**
* Constructor
*
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()
2017-01-17 11:28:13 -05:00
{
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
}
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): mixed
2017-01-17 11:21:07 -05:00
{
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
{
$this->validateKeys($keys);
$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|DateInterval|null $expires
2020-05-07 17:17:03 -04:00
* @return bool
2017-01-17 11:21:07 -05:00
*/
public function set(string $key, mixed $value, int|DateInterval|null $expires = NULL): 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
}
/**
* Set multiple cache values
*
* @param array $items
2021-02-19 12:18:38 -05:00
* @param DateInterval|int|null $expires
* @return bool
*/
2021-02-19 12:18:38 -05:00
public function setMultiple(array $items, DateInterval|int|null $expires = NULL): bool
{
$this->validateKeys($items, TRUE);
2020-05-08 18:58:25 -04:00
$errorKeys = ($expires === NULL)
? apcu_store($items)
2021-02-19 12:18:38 -05:00
: apcu_store($items, NULL, $this->getTTLFromExpiration($expires));
2020-05-08 18:58:25 -04:00
return empty($errorKeys);
}
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
{
$this->validateKeys($keys);
2020-05-08 18:58:25 -04:00
$failedToDelete = apcu_delete($keys);
return empty($failedToDelete);
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
}
$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
*
2021-02-19 12:18:38 -05:00
* @param DateInterval|int|null $expires
2020-05-07 17:17:03 -04:00
* @return int
*/
2021-02-19 12:18:38 -05:00
protected function getTTLFromExpiration(DateInterval|int|null $expires): int
2020-05-07 17:17:03 -04:00
{
2021-02-19 12:18:38 -05:00
if ($expires instanceof DateInterval)
{
return $expires->s;
}
$ttl = (int)$expires - time();
2020-05-07 17:17:03 -04:00
return ($ttl < 0) ? 0 : $ttl;
}
2017-01-17 11:21:07 -05:00
}