Some checks failed
Gitea - aviat/banker/pipeline/head There was a failure building this commit
181 lines
3.5 KiB
PHP
181 lines
3.5 KiB
PHP
<?php declare(strict_types=1);
|
|
/**
|
|
* Banker
|
|
*
|
|
* A Caching library implementing psr/cache (PSR 6)
|
|
*
|
|
* PHP version 7.2
|
|
*
|
|
* @package Banker
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
* @copyright 2016 - 2019 Timothy J. Warren
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
* @version 3.0.0
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
*/
|
|
namespace Aviat\Banker\Driver;
|
|
|
|
use Aviat\Banker\Exception\CacheException;
|
|
|
|
use Memcached;
|
|
use MemcachedException;
|
|
|
|
/**
|
|
* Memcached cache backend
|
|
*/
|
|
class MemcachedDriver extends AbstractDriver {
|
|
|
|
/**
|
|
* @var Memcached
|
|
*/
|
|
private ?Memcached $conn;
|
|
|
|
/**
|
|
* Driver for PHP Memcache extension
|
|
*
|
|
* @codeCoverageIgnore
|
|
* @param array $config
|
|
* @param array $options
|
|
* @throws CacheException
|
|
*/
|
|
public function __construct(
|
|
array $config = ['host' => '127.0.0.1', 'port' => '11211'],
|
|
array $options = []
|
|
)
|
|
{
|
|
if ( ! class_exists('Memcached'))
|
|
{
|
|
throw new CacheException('Memcached driver requires memcached extension');
|
|
}
|
|
|
|
try
|
|
{
|
|
$this->conn = new Memcached();
|
|
$this->conn->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
|
|
$this->conn->addServer($config['host'], (int) $config['port']);
|
|
|
|
if ( ! empty($options))
|
|
{
|
|
$this->conn->setOptions($options);
|
|
}
|
|
}
|
|
catch (MemcachedException $e)
|
|
{
|
|
// Rewrite MemcachedException as a CacheException to
|
|
// match the requirements of the interface
|
|
throw new CacheException($e->getMessage(), $e->getCode(), $e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Disconnect from memcached server
|
|
* @codeCoverageIgnore
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
$this->conn->quit();
|
|
}
|
|
|
|
/**
|
|
* See if a key currently exists in the cache
|
|
*
|
|
* @param string $key
|
|
* @return bool
|
|
*/
|
|
public function exists(string $key): bool
|
|
{
|
|
$this->conn->get($key);
|
|
$resultFlag = $this->conn->getResultCode();
|
|
|
|
return ($resultFlag !== Memcached::RES_NOTFOUND);
|
|
}
|
|
|
|
/**
|
|
* Get the value for the selected cache key
|
|
*
|
|
* @param string $key
|
|
* @return mixed
|
|
*/
|
|
public function get(string $key)
|
|
{
|
|
return $this->conn->get($key);
|
|
}
|
|
|
|
/**
|
|
* Retrieve a set of values by their cache key
|
|
*
|
|
* @param string[] $keys
|
|
* @return array
|
|
*/
|
|
public function getMultiple(array $keys = []): array
|
|
{
|
|
$response = $this->conn->getMulti($keys);
|
|
return (is_array($response)) ? $response : [];
|
|
}
|
|
|
|
/**
|
|
* Set a cached value
|
|
*
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @param int $expires
|
|
* @return bool
|
|
*/
|
|
public function set(string $key, $value, int $expires = 0): bool
|
|
{
|
|
return $this->conn->set($key, $value, $expires);
|
|
}
|
|
|
|
/**
|
|
* Remove an item from the cache
|
|
*
|
|
* @param string $key
|
|
* @return boolean
|
|
*/
|
|
public function delete(string $key): bool
|
|
{
|
|
return $this->conn->delete($key);
|
|
}
|
|
|
|
/**
|
|
* Remove multiple items from the cache
|
|
*
|
|
* @param string[] $keys
|
|
* @return boolean
|
|
*/
|
|
public function deleteMultiple(array $keys = []): bool
|
|
{
|
|
$deleted = $this->conn->deleteMulti($keys);
|
|
return ($keys <=> $deleted) === 0;
|
|
}
|
|
|
|
/**
|
|
* Empty the cache
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function flush(): bool
|
|
{
|
|
return $this->conn->flush();
|
|
}
|
|
|
|
/**
|
|
* 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))
|
|
{
|
|
return $this->conn->touch($key, $expires);
|
|
}
|
|
|
|
$this->getLogger()->log('warning','Tried to set expiration on a key that does not exist');
|
|
|
|
return FALSE;
|
|
}
|
|
}
|