Update Redis cache driver to use PHP-only library, removing the dependence on an extension:

This commit is contained in:
Timothy Warren 2016-07-22 17:22:00 -04:00
parent 0632ebba66
commit 4772c6df95
6 changed files with 29 additions and 49 deletions

View File

@ -3,6 +3,7 @@
################################################################################
# Host or socket to connect to
# Socket must be prefixed with 'unix:'
host = "127.0.0.1"
# Connection port

View File

@ -13,6 +13,7 @@
"filp/whoops": "2.0.*",
"guzzlehttp/guzzle": "6.*",
"monolog/monolog": "1.*",
"predis/predis": "1.1.*",
"psr/http-message": "~1.0",
"psr/log": "~1.0",
"robmorgan/phinx": "0.4.*",

View File

@ -13,8 +13,11 @@
namespace Aviat\Ion\Cache\Driver;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Cache\CacheDriverInterface;
class RedisDriver implements \Aviat\Ion\Cache\CacheDriverInterface {
use Predis\Client;
class RedisDriver implements CacheDriverInterface {
/**
* The redis extension class instance
@ -30,33 +33,20 @@ class RedisDriver implements \Aviat\Ion\Cache\CacheDriverInterface {
$config = $container->get('config');
$redisConfig = $config->get('redis');
$this->redis = new \Redis();
(array_key_exists('port', $redisConfig))
? $this->redis->pconnect($redisConfig['host'], $redisConfig['port'])
: $this->redis->pconnect($redisConfig['host']);
// If there is a password, authorize
if (array_key_exists('password', $redisConfig))
if (array_key_exists('password', $redisConfig) && $redisConfig['password'] === '')
{
$this->redis->auth($redisConfig['password']);
unset($redisConfig['password']);
}
// If there is a database selected, connect to the specified database
if (array_key_exists('database', $redisConfig))
{
$this->redis->select($redisConfig['database']);
}
$this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
$this->redis = new Client($redisConfig);
}
/**
* Destructor to disconnect from redis
* Disconnect from redis
*/
public function __destruct()
{
$this->redis->close();
$this->redis = null;
}
/**
@ -67,7 +57,7 @@ class RedisDriver implements \Aviat\Ion\Cache\CacheDriverInterface {
*/
public function get($key)
{
return $this->redis->get($key);
return unserialize($this->redis->get($key));
}
/**
@ -79,7 +69,7 @@ class RedisDriver implements \Aviat\Ion\Cache\CacheDriverInterface {
*/
public function set($key, $value)
{
$this->redis->set($key, $value);
$this->redis->set($key, serialize($value));
return $this;
}

View File

@ -18,7 +18,9 @@ trait CacheDriverBase {
public function testDriverGetSet()
{
$this->driver->set('foo', $this->foo);
$this->driver->set('bar', 'baz');
$this->assertEquals($this->driver->get('foo'), $this->foo);
$this->assertEquals($this->driver->get('bar'), 'baz');
}
public function testInvalidate()

View File

@ -15,24 +15,17 @@ class CacheRedisDriverTestTwo extends AnimeClient_TestCase {
{
parent::setUp();
if ( ! class_exists('Redis'))
{
$this->markTestSkipped('Redis extension not installed');
}
else
{
// Setup config with port and password
$container = new Container();
$container->set('config', new Config([
'redis' => [
'host' => 'localhost',
'port' => 6379,
'password' => '',
'database' => 13,
]
]));
$this->driver = new RedisDriver($container);
}
// Setup config with port and password
$container = new Container();
$container->set('config', new Config([
'redis' => [
'host' => 'localhost',
'port' => 6379,
'password' => '',
'database' => 13,
]
]));
$this->driver = new RedisDriver($container);
}
public function tearDown()

View File

@ -13,14 +13,7 @@ class CacheRedisDriverTest extends AnimeClient_TestCase {
{
parent::setUp();
if ( ! class_exists('Redis'))
{
$this->markTestSkipped('Redis extension not installed');
}
else
{
$this->driver = new RedisDriver($this->container);
}
$this->driver = new RedisDriver($this->container);
}
public function tearDown()