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
|
|
|
*
|
2020-05-07 19:00:38 -04:00
|
|
|
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
|
2016-08-31 12:18:46 -04:00
|
|
|
*
|
2021-02-05 17:05:56 -05:00
|
|
|
* PHP version 7.4+
|
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>
|
2021-02-05 17:05:56 -05:00
|
|
|
* @copyright 2016 - 2021 Timothy J. Warren
|
2016-08-31 12:18:46 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2021-02-05 17:05:56 -05:00
|
|
|
* @version 3.2.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;
|
|
|
|
|
2019-12-10 11:00:46 -05:00
|
|
|
use Aviat\Banker\Exception\CacheException;
|
2016-09-05 16:43:37 -04:00
|
|
|
use Predis\Client;
|
|
|
|
|
2021-02-18 19:23:20 -05:00
|
|
|
use DateInterval;
|
|
|
|
|
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
|
|
|
|
*
|
2019-12-10 11:00:46 -05:00
|
|
|
* @var Client
|
2016-09-05 16:43:37 -04:00
|
|
|
*/
|
2021-02-18 19:23:20 -05:00
|
|
|
protected Client $conn;
|
2016-09-05 16:43:37 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RedisDriver constructor.
|
|
|
|
*
|
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 - 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
|
|
|
{
|
2019-12-10 11:00:46 -05:00
|
|
|
if ( ! class_exists(Client::class))
|
2016-09-05 16:43:37 -04:00
|
|
|
{
|
2019-12-10 11:00:46 -05:00
|
|
|
throw new CacheException('The redis driver requires the predis/predis composer package to be installed.');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-07 17:17:03 -04:00
|
|
|
* Disconnect from redis server
|
|
|
|
* @codeCoverageIgnore
|
2016-09-05 16:43:37 -04:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
2021-02-18 19:23:20 -05:00
|
|
|
public function get(string $key): mixed
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a cached value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
2021-02-18 19:23:20 -05:00
|
|
|
* @param int|null $expires
|
2020-05-07 17:17:03 -04:00
|
|
|
* @return bool
|
2016-09-05 16:43:37 -04:00
|
|
|
*/
|
2021-02-18 19:23:20 -05:00
|
|
|
public function set(string $key, mixed $value, DateInterval|int|null $expires = NULL): bool
|
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
|
|
|
|
2020-05-08 18:58:25 -04:00
|
|
|
$status = ($expires !== NULL)
|
|
|
|
? $this->conn->set($key, $value, 'EX', $expires)
|
|
|
|
: $this->conn->set($key, $value);
|
2016-09-06 20:26:28 -04:00
|
|
|
|
2020-05-08 18:58:25 -04:00
|
|
|
return (string)$status === 'OK';
|
2016-09-05 16:43:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2020-05-08 18:58:25 -04:00
|
|
|
// This call returns the number of keys deleted
|
|
|
|
return $this->conn->del([$key]) === 1;
|
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
|
|
|
{
|
2021-02-05 13:26:01 -05:00
|
|
|
$this->validateKeys($keys);
|
|
|
|
|
|
|
|
$res = $this->conn->del(...array_values($keys));
|
2020-05-07 17:17:03 -04:00
|
|
|
return $res === count($keys);
|
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
|
|
|
{
|
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
|
|
|
}
|