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 # Host or socket to connect to
# Socket must be prefixed with 'unix:'
host = "127.0.0.1" host = "127.0.0.1"
# Connection port # Connection port

View File

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

View File

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

View File

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

View File

@ -15,12 +15,6 @@ class CacheRedisDriverTestTwo extends AnimeClient_TestCase {
{ {
parent::setUp(); parent::setUp();
if ( ! class_exists('Redis'))
{
$this->markTestSkipped('Redis extension not installed');
}
else
{
// Setup config with port and password // Setup config with port and password
$container = new Container(); $container = new Container();
$container->set('config', new Config([ $container->set('config', new Config([
@ -33,7 +27,6 @@ class CacheRedisDriverTestTwo extends AnimeClient_TestCase {
])); ]));
$this->driver = new RedisDriver($container); $this->driver = new RedisDriver($container);
} }
}
public function tearDown() public function tearDown()
{ {

View File

@ -13,15 +13,8 @@ class CacheRedisDriverTest extends AnimeClient_TestCase {
{ {
parent::setUp(); 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() public function tearDown()
{ {