From fa22f7b49379a09cb3b37346161546d71317a0b4 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 22 Jul 2016 17:22:00 -0400 Subject: [PATCH] Update Redis cache driver to use PHP-only library, removing the dependence on an extension: --- app/config/redis.toml.example | 1 + composer.json | 1 + src/Aviat/Ion/Cache/Driver/RedisDriver.php | 36 ++++++++------------- tests/Ion/Cache/Driver/CacheDriverBase.php | 2 ++ tests/Ion/Cache/Driver/RedisDriver2Test.php | 29 +++++++---------- tests/Ion/Cache/Driver/RedisDriverTest.php | 9 +----- 6 files changed, 29 insertions(+), 49 deletions(-) diff --git a/app/config/redis.toml.example b/app/config/redis.toml.example index c45ea3c1..a3f7b996 100644 --- a/app/config/redis.toml.example +++ b/app/config/redis.toml.example @@ -3,6 +3,7 @@ ################################################################################ # Host or socket to connect to +# Socket must be prefixed with 'unix:' host = "127.0.0.1" # Connection port diff --git a/composer.json b/composer.json index 018dcfe7..bf012bc2 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/src/Aviat/Ion/Cache/Driver/RedisDriver.php b/src/Aviat/Ion/Cache/Driver/RedisDriver.php index 3fedc6e0..db265e13 100644 --- a/src/Aviat/Ion/Cache/Driver/RedisDriver.php +++ b/src/Aviat/Ion/Cache/Driver/RedisDriver.php @@ -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 @@ -29,34 +32,21 @@ 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; } diff --git a/tests/Ion/Cache/Driver/CacheDriverBase.php b/tests/Ion/Cache/Driver/CacheDriverBase.php index e0ba7a94..d1e333aa 100644 --- a/tests/Ion/Cache/Driver/CacheDriverBase.php +++ b/tests/Ion/Cache/Driver/CacheDriverBase.php @@ -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() diff --git a/tests/Ion/Cache/Driver/RedisDriver2Test.php b/tests/Ion/Cache/Driver/RedisDriver2Test.php index b6b6ecaa..8bcbb0b9 100644 --- a/tests/Ion/Cache/Driver/RedisDriver2Test.php +++ b/tests/Ion/Cache/Driver/RedisDriver2Test.php @@ -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() diff --git a/tests/Ion/Cache/Driver/RedisDriverTest.php b/tests/Ion/Cache/Driver/RedisDriverTest.php index 9200dc61..3ae45a9a 100644 --- a/tests/Ion/Cache/Driver/RedisDriverTest.php +++ b/tests/Ion/Cache/Driver/RedisDriverTest.php @@ -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()