banker/src/Driver/ApcuDriver.php

157 lines
2.8 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
*
2018-11-15 16:38:36 -05:00
* PHP version 7.1
2017-01-17 11:21:07 -05:00
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
2018-11-15 16:38:36 -05:00
* @copyright 2016 - 2018 Timothy J. Warren
2017-01-17 11:21:07 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2018-11-15 16:38:36 -05:00
* @version 2.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;
/**
* Memcached cache backend
*/
class ApcuDriver extends AbstractDriver {
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
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
}
2017-01-17 11:28:13 -05:00
/**
* Destructor
*/
public function __destruct()
{
2017-03-01 13:04:00 -05:00
// noop
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
{
2019-12-10 11:00:46 -05: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)
{
2019-12-10 11:00:46 -05: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
{
$status = FALSE;
2019-12-10 11:00:46 -05: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
* @return DriverInterface
*/
public function set(string $key, $value, int $expires = 0): DriverInterface
{
2019-12-10 11:00:46 -05:00
if ( ! \apcu_exists($key))
2017-01-17 11:21:07 -05:00
{
2019-12-10 11:00:46 -05:00
\apcu_add($key, $value, $expires);
2017-01-17 11:21:07 -05:00
}
else
{
2019-12-10 11:00:46 -05:00
\apcu_store($key, $value, $expires);
2017-01-17 11:21:07 -05:00
}
return $this;
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
public function delete(string $key): bool
{
2019-12-10 11:00:46 -05:00
return (bool) \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
{
2019-12-10 11:00:46 -05:00
return (bool) \apcu_delete($keys);
2017-01-17 11:21:07 -05:00
}
/**
* Empty the cache
*
* @return boolean
*/
public function flush(): bool
{
2019-12-10 11:00:46 -05: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);
2019-12-10 11:00:46 -05:00
return \apcu_store($key, $value, $expires);
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;
}
}