Add APCu driver

This commit is contained in:
Timothy Warren 2017-01-17 11:21:07 -05:00
parent 4f6ae064da
commit 876858b515
2 changed files with 161 additions and 0 deletions

133
src/Driver/ApcuDriver.php Normal file
View File

@ -0,0 +1,133 @@
<?php declare(strict_types=1);
/**
* Banker
*
* A Caching library implementing psr/cache
*
* PHP version 7.0
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 1.0.0
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker\Driver;
use Aviat\Banker\Exception\CacheException;
/**
* Memcached cache backend
*/
class ApcuDriver extends AbstractDriver {
/**
* 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
{
return apcu_fetch($keys);
}
/**
* 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 apcu_delete($key);
}
/**
* Remove multiple items from the cache
*
* @param string[] $keys
* @return boolean
*/
public function deleteMultiple(array $keys = []): bool
{
return 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()->warn("Tried to set expiration on a key that does not exist");
return FALSE;
}
}

View File

@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/**
* Banker
*
* A Caching library implementing psr/cache
*
* PHP version 7.0
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 1.0.0
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker\Tests\Driver;
use Aviat\Banker\Driver\ApcuDriver;
class ApcuDriverTest extends DriverTestBase {
public function setup()
{
$this->driver = new ApcuDriver();
$this->driver->flush();
}
}