From ea1b63265b2445381bcd4729235b86452d137f77 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 8 Apr 2016 14:25:45 -0400 Subject: [PATCH] Add Redis Cache driver --- app/config/config.toml.example | 2 +- app/config/redis.toml.example | 15 +++ .../AnimeClient/Model/AnimeCollection.php | 7 +- src/Aviat/Ion/Cache/CacheDriverInterface.php | 62 +++++----- src/Aviat/Ion/Cache/Driver/RedisDriver.php | 108 ++++++++++++++++++ src/Aviat/Ion/Cache/Driver/SQLDriver.php | 9 +- tests/AnimeClient_TestCase.php | 4 + tests/Ion/Cache/Driver/RedisDriverTest.php | 18 +++ tests/Ion/Cache/Driver/SQLDriverTest.php | 2 +- 9 files changed, 183 insertions(+), 44 deletions(-) create mode 100644 app/config/redis.toml.example create mode 100644 src/Aviat/Ion/Cache/Driver/RedisDriver.php create mode 100644 tests/Ion/Cache/Driver/RedisDriverTest.php diff --git a/app/config/config.toml.example b/app/config/config.toml.example index 9e311655..3d9f2543 100644 --- a/app/config/config.toml.example +++ b/app/config/config.toml.example @@ -14,7 +14,7 @@ show_anime_collection = true # do you wish to show the manga collection? show_manga_collection = false -# cache driver for api calls (SQLDriver, RedisDriver, MemcacheDriver) +# cache driver for api calls (SQLDriver, RedisDriver) cache_driver = SQLDriver # path to public directory on the server diff --git a/app/config/redis.toml.example b/app/config/redis.toml.example new file mode 100644 index 00000000..c45ea3c1 --- /dev/null +++ b/app/config/redis.toml.example @@ -0,0 +1,15 @@ +################################################################################ +# Redis Cache Configuration # +################################################################################ + +# Host or socket to connect to +host = "127.0.0.1" + +# Connection port +#port = 6379 + +# Connection password +#password = "" + +# Database number +database = 13 \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Model/AnimeCollection.php b/src/Aviat/AnimeClient/Model/AnimeCollection.php index cf7e32d4..a96a2eb8 100644 --- a/src/Aviat/AnimeClient/Model/AnimeCollection.php +++ b/src/Aviat/AnimeClient/Model/AnimeCollection.php @@ -55,10 +55,11 @@ class AnimeCollection extends DB { // Is database valid? If not, set a flag so the // app can be run without a valid database - $db_file_name = $this->db_config['collection']['file']; - if ($db_file_name !== ':memory:') + if ($this->db_config['collection']['type'] === 'sqlite') { - if (file_exists($db_file_name)) + $db_file_name = $this->db_config['collection']['file']; + + if ($db_file_name !== ':memory:' && file_exists($db_file_name)) { $db_file = file_get_contents($db_file_name); $this->valid_database = (strpos($db_file, 'SQLite format 3') === 0); diff --git a/src/Aviat/Ion/Cache/CacheDriverInterface.php b/src/Aviat/Ion/Cache/CacheDriverInterface.php index ed64151c..9813f9bc 100644 --- a/src/Aviat/Ion/Cache/CacheDriverInterface.php +++ b/src/Aviat/Ion/Cache/CacheDriverInterface.php @@ -4,10 +4,10 @@ * * Building blocks for web development * - * @package Ion - * @author Timothy J. Warren + * @package Ion + * @author Timothy J. Warren * @copyright Copyright (c) 2015 - 2016 - * @license MIT + * @license MIT */ namespace Aviat\Ion\Cache; @@ -16,36 +16,36 @@ namespace Aviat\Ion\Cache; * Interface for cache drivers */ interface CacheDriverInterface { - /** - * Retreive a value from the cache backend - * - * @param string $key - * @return mixed - */ - public function get($key); + /** + * Retreive a value from the cache backend + * + * @param string $key + * @return mixed + */ + public function get($key); - /** - * Set a cached value - * - * @param string $key - * @param mixed $value - * @return CacheDriverInterface - */ - public function set($key, $value); + /** + * Set a cached value + * + * @param string $key + * @param mixed $value + * @return CacheDriverInterface + */ + public function set($key, $value); - /** - * Invalidate a cached value - * - * @param string $key - * @return CacheDriverInterface - */ - public function invalidate($key); + /** + * Invalidate a cached value + * + * @param string $key + * @return CacheDriverInterface + */ + public function invalidate($key); - /** - * Clear the contents of the cache - * - * @return void - */ - public function invalidateAll(); + /** + * Clear the contents of the cache + * + * @return void + */ + public function invalidateAll(); } // End of CacheDriverInterface.php \ No newline at end of file diff --git a/src/Aviat/Ion/Cache/Driver/RedisDriver.php b/src/Aviat/Ion/Cache/Driver/RedisDriver.php new file mode 100644 index 00000000..bc91d2ab --- /dev/null +++ b/src/Aviat/Ion/Cache/Driver/RedisDriver.php @@ -0,0 +1,108 @@ +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)) + { + $this->redis->auth($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); + } + + /** + * Destructor to disconnect from redis + */ + public function __destruct() + { + $this->redis->close(); + } + + /** + * Retreive a value from the cache backend + * + * @param string $key + * @return mixed + */ + public function get($key) + { + return $this->redis->get($key); + } + + /** + * Set a cached value + * + * @param string $key + * @param mixed $value + * @return CacheDriverInterface + */ + public function set($key, $value) + { + $this->redis->set($key, $value); + return $this; + } + + /** + * Invalidate a cached value + * + * @param string $key + * @return CacheDriverInterface + */ + public function invalidate($key) + { + $this->redis->del($key); + return $this; + } + + /** + * Clear the contents of the cache + * + * @return void + */ + public function invalidateAll() + { + $this->redis->flushDB(); + } +} +// End of RedisDriver.php \ No newline at end of file diff --git a/src/Aviat/Ion/Cache/Driver/SQLDriver.php b/src/Aviat/Ion/Cache/Driver/SQLDriver.php index a68ded41..77ebae65 100644 --- a/src/Aviat/Ion/Cache/Driver/SQLDriver.php +++ b/src/Aviat/Ion/Cache/Driver/SQLDriver.php @@ -72,14 +72,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { 'value' => serialize($value), ]); - /*if ( ! empty($this->get($key))) - { - $this->db->update('cache'); - } - else*/ - { - $this->db->insert('cache'); - } + $this->db->insert('cache'); return $this; } diff --git a/tests/AnimeClient_TestCase.php b/tests/AnimeClient_TestCase.php index f4a3b178..f4a296dc 100644 --- a/tests/AnimeClient_TestCase.php +++ b/tests/AnimeClient_TestCase.php @@ -71,6 +71,10 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase { 'routes' => [ ] + ], + 'redis' => [ + 'host' => 'localhost', + 'database' => 13 ] ]; diff --git a/tests/Ion/Cache/Driver/RedisDriverTest.php b/tests/Ion/Cache/Driver/RedisDriverTest.php new file mode 100644 index 00000000..0961e7e1 --- /dev/null +++ b/tests/Ion/Cache/Driver/RedisDriverTest.php @@ -0,0 +1,18 @@ +driver = new RedisDriver($this->container); + } +} \ No newline at end of file diff --git a/tests/Ion/Cache/Driver/SQLDriverTest.php b/tests/Ion/Cache/Driver/SQLDriverTest.php index 6eb2c349..d23d9ec1 100644 --- a/tests/Ion/Cache/Driver/SQLDriverTest.php +++ b/tests/Ion/Cache/Driver/SQLDriverTest.php @@ -1,6 +1,6 @@