banker/src/Driver/ApcuDriver.php

134 lines
2.3 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
*
* 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;
}
}