2016-10-19 09:57:06 -04:00
|
|
|
<?php declare(strict_types=1);
|
2016-08-31 12:18:46 -04:00
|
|
|
/**
|
2016-09-05 16:43:37 -04:00
|
|
|
* Banker
|
2016-08-31 12:18:46 -04:00
|
|
|
*
|
|
|
|
* A Caching library implementing psr/cache
|
|
|
|
*
|
2016-10-19 09:57:06 -04:00
|
|
|
* PHP version 7.0
|
2016-08-31 12:18:46 -04:00
|
|
|
*
|
2016-09-05 16:43:37 -04:00
|
|
|
* @package Banker
|
2016-08-31 12:18:46 -04:00
|
|
|
* @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
|
2016-09-05 16:43:37 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
2016-08-31 12:18:46 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Banker\Driver;
|
|
|
|
|
2016-09-05 16:43:37 -04:00
|
|
|
use Aviat\Banker\Exception\CacheException;
|
|
|
|
use Predis\Client;
|
|
|
|
|
2016-08-31 12:18:46 -04:00
|
|
|
/**
|
|
|
|
* Redis cache backend
|
|
|
|
*/
|
2016-09-06 17:03:43 -04:00
|
|
|
class RedisDriver extends AbstractDriver {
|
2016-09-05 16:43:37 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The object encapsulating the connection to the Redis server
|
|
|
|
*
|
|
|
|
* @var Predis\Client
|
|
|
|
*/
|
|
|
|
protected $conn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RedisDriver constructor.
|
|
|
|
*
|
|
|
|
* @param array $config
|
2016-09-06 20:57:24 -04:00
|
|
|
* @param array $options - Predis library connection options
|
2016-09-05 16:43:37 -04:00
|
|
|
* @throws CacheException
|
|
|
|
*/
|
2016-09-06 20:57:24 -04:00
|
|
|
public function __construct(array $config = [], array $options = [])
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
2016-09-06 20:26:28 -04:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-09-05 16:43:37 -04:00
|
|
|
if ( ! class_exists('Predis\\Client'))
|
|
|
|
{
|
|
|
|
throw new CacheException("The redis driver requires the predis/predis composer package to be installed.");
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-09-05 16:43:37 -04:00
|
|
|
|
2016-09-06 20:57:24 -04:00
|
|
|
$this->conn = new Client($config, $options);
|
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 (bool) $this->conn->exists($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
$raw = $this->conn->get($key);
|
2016-10-19 09:57:06 -04:00
|
|
|
return unserialize($raw);
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
foreach($keys as $key)
|
|
|
|
{
|
|
|
|
$output[$key] = $this->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a cached value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @param int $expires
|
|
|
|
* @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-10-19 09:57:06 -04:00
|
|
|
$value = serialize($value);
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2016-09-06 17:03:43 -04:00
|
|
|
if ($expires !== 0)
|
|
|
|
{
|
2016-09-06 20:26:28 -04:00
|
|
|
$this->conn->set($key, $value, "EX", $expires);
|
2016-09-06 17:03:43 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->conn->set($key, $value);
|
|
|
|
}
|
2016-09-06 20:26:28 -04:00
|
|
|
|
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
|
|
|
{
|
|
|
|
return (bool) $this->conn->del($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2017-01-17 12:34:50 -05:00
|
|
|
$res = $this->conn->del(...$keys);
|
2016-09-05 16:43:37 -04:00
|
|
|
return $res === count($keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2016-10-19 09:57:06 -04:00
|
|
|
return (bool) $this->conn->flushdb();
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the expiration timestamp of a key
|
|
|
|
*
|
|
|
|
* @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-05 16:43:37 -04:00
|
|
|
{
|
|
|
|
return (bool) $this->conn->expireat($key, $expires);
|
|
|
|
}
|
2016-08-31 12:18:46 -04:00
|
|
|
}
|