2016-10-19 09:57:06 -04:00
|
|
|
<?php declare(strict_types=1);
|
2016-09-05 16:43:37 -04:00
|
|
|
/**
|
|
|
|
* Banker
|
|
|
|
*
|
|
|
|
* A Caching library implementing psr/cache
|
|
|
|
*
|
2019-12-10 11:02:02 -05:00
|
|
|
* PHP version 7.2
|
2016-09-05 16:43:37 -04:00
|
|
|
*
|
|
|
|
* @package Banker
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2019-12-10 11:02:02 -05:00
|
|
|
* @copyright 2016 - 2019 Timothy J. Warren
|
2016-09-05 16:43:37 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-10 11:02:02 -05:00
|
|
|
* @version 3.0.0
|
2016-09-05 16:43:37 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
|
|
*/
|
|
|
|
namespace Aviat\Banker\Driver;
|
|
|
|
|
|
|
|
use Aviat\Banker\Exception\CacheException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Memcached cache backend
|
|
|
|
*/
|
2016-09-06 17:03:43 -04:00
|
|
|
class MemcachedDriver extends AbstractDriver {
|
2016-09-05 16:43:37 -04:00
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
/**
|
|
|
|
* @var \Memcached
|
|
|
|
*/
|
|
|
|
private $conn;
|
|
|
|
|
2016-09-05 16:43:37 -04:00
|
|
|
/**
|
|
|
|
* Driver for PHP Memcache extension
|
|
|
|
*
|
2018-11-15 16:37:50 -05:00
|
|
|
* @codeCoverageIgnore
|
2016-09-05 16:43:37 -04:00
|
|
|
* @param array $config
|
2016-09-06 20:57:24 -04:00
|
|
|
* @param array $options
|
2016-09-05 16:43:37 -04:00
|
|
|
* @throws CacheException
|
|
|
|
*/
|
2017-03-01 12:04:01 -05:00
|
|
|
public function __construct(
|
2018-11-15 16:37:50 -05:00
|
|
|
array $config = ['host' => '127.0.0.1', 'port' => '11211'],
|
2017-03-01 12:04:01 -05:00
|
|
|
array $options = []
|
|
|
|
)
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
|
|
|
if ( ! class_exists('Memcached'))
|
|
|
|
{
|
2018-11-15 16:37:50 -05:00
|
|
|
throw new CacheException('Memcached driver requires memcached extension');
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
try
|
|
|
|
{
|
2019-12-10 11:00:46 -05:00
|
|
|
$this->conn = new \Memcached();
|
|
|
|
$this->conn->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
|
2016-10-19 09:57:06 -04:00
|
|
|
$this->conn->addServer($config['host'], (int) $config['port']);
|
2016-09-06 20:57:24 -04:00
|
|
|
|
|
|
|
if ( ! empty($options))
|
|
|
|
{
|
|
|
|
$this->conn->setOptions($options);
|
|
|
|
}
|
2016-09-06 17:03:43 -04:00
|
|
|
}
|
2019-12-10 11:00:46 -05:00
|
|
|
catch (\MemcachedException $e)
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
2016-09-06 20:26:28 -04:00
|
|
|
// Rewrite MemcachedException as a CacheException to
|
2016-09-06 17:03:43 -04:00
|
|
|
// match the requirements of the interface
|
2016-09-06 20:26:28 -04:00
|
|
|
throw new CacheException($e->getMessage(), $e->getCode(), $e);
|
2016-09-06 17:03:43 -04:00
|
|
|
}
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect from memcached server
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->conn->quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if a key currently exists in the cache
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function exists(string $key): bool
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
|
|
|
return $this->conn->get($key) !== FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value for the selected cache key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function get(string $key)
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
|
|
|
return $this->conn->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a set of values by their cache key
|
|
|
|
*
|
|
|
|
* @param string[] $keys
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function getMultiple(array $keys = []): array
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
2017-03-01 11:42:42 -05:00
|
|
|
$return = $this->conn->getMulti($keys);
|
|
|
|
return ($return === FALSE) ? [] : $return;
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a cached value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
2016-09-06 11:43:25 -04:00
|
|
|
* @param int $expires
|
2016-09-05 16:43:37 -04:00
|
|
|
* @return DriverInterface
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function set(string $key, $value, int $expires = 0): DriverInterface
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
2016-09-06 17:03:43 -04:00
|
|
|
if ( ! $this->exists($key))
|
|
|
|
{
|
|
|
|
$this->conn->add($key, $value, $expires);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->conn->replace($key, $value, $expires);
|
|
|
|
}
|
|
|
|
|
2016-09-05 16:43:37 -04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the cache
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function delete(string $key): bool
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
2018-11-15 16:37:50 -05:00
|
|
|
return (bool) $this->conn->delete($key);
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove multiple items from the cache
|
|
|
|
*
|
|
|
|
* @param string[] $keys
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function deleteMultiple(array $keys = []): bool
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
2018-11-15 16:37:50 -05:00
|
|
|
$deleted = $this->conn->deleteMulti($keys);
|
|
|
|
return ($keys <=> $deleted) === 0;
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty the cache
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function flush(): bool
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
|
|
|
return $this->conn->flush();
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
/**
|
|
|
|
* Set the specified key to expire at the given time
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param int $expires
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-19 09:57:06 -04:00
|
|
|
public function expiresAt(string $key, int $expires): bool
|
2016-09-06 17:03:43 -04:00
|
|
|
{
|
|
|
|
if ($this->exists($key))
|
|
|
|
{
|
|
|
|
return $this->conn->touch($key, $expires);
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2018-11-15 16:37:50 -05:00
|
|
|
$this->getLogger()->log('warning','Tried to set expiration on a key that does not exist');
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-09-06 11:43:25 -04:00
|
|
|
}
|