2016-08-31 12:18:46 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-09-05 16:43:37 -04:00
|
|
|
* Banker
|
2016-08-31 12:18:46 -04:00
|
|
|
*
|
|
|
|
* A Caching library implementing psr/cache
|
|
|
|
*
|
|
|
|
* PHP version 5.6
|
|
|
|
*
|
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
|
|
|
|
*/
|
|
|
|
class RedisDriver extends Driver {
|
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
|
|
|
|
* @throws CacheException
|
|
|
|
*/
|
|
|
|
public function __construct(array $config = [])
|
|
|
|
{
|
|
|
|
if ( ! class_exists('Predis\\Client'))
|
|
|
|
{
|
|
|
|
throw new CacheException("The redis driver requires the predis/predis composer package to be installed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->conn = new Client($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect from memcached server
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->conn->quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if a key currently exists in the cache
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists($key)
|
|
|
|
{
|
|
|
|
return (bool) $this->conn->exists($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value for the selected cache key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
$raw = $this->conn->get($key);
|
|
|
|
return \unserialize($raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a set of values by their cache key
|
|
|
|
*
|
|
|
|
* @param string[] $keys
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMultiple(array $keys = [])
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function set($key, $value, $expires = 0)
|
|
|
|
{
|
|
|
|
$value = \serialize($value);
|
|
|
|
|
|
|
|
$this->conn->set($key, $value, "EX {$expires}");
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the cache
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function delete($key)
|
|
|
|
{
|
|
|
|
return (bool) $this->conn->del($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove multiple items from the cache
|
|
|
|
*
|
|
|
|
* @param string[] $keys
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function deleteMultiple(array $keys = [])
|
|
|
|
{
|
|
|
|
$res = \call_user_func_array([$this->conn, 'del'], $keys);
|
|
|
|
return $res === count($keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty the cache
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function flush()
|
|
|
|
{
|
|
|
|
return $this->conn->flushdb();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the expiration timestamp of a key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param int $expires
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function expiresAt($key, $expires)
|
|
|
|
{
|
|
|
|
return (bool) $this->conn->expireat($key, $expires);
|
|
|
|
}
|
2016-08-31 12:18:46 -04:00
|
|
|
}
|