From 7d6c6fe2a09ad6b08764d28a192109f466c4935c Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 12 Apr 2016 12:05:42 -0400 Subject: [PATCH] Add NullDriver for cache layer, for the sake of testing, or config without a cache --- app/config/config.toml.example | 4 +- src/Aviat/Ion/Cache/CacheManager.php | 2 +- src/Aviat/Ion/Cache/Driver/NullDriver.php | 83 +++++++++++++++++++++++ tests/Ion/Cache/Driver/NullDriverTest.php | 17 +++++ 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/Aviat/Ion/Cache/Driver/NullDriver.php create mode 100644 tests/Ion/Cache/Driver/NullDriverTest.php diff --git a/app/config/config.toml.example b/app/config/config.toml.example index 3d9f2543..9c3ec657 100644 --- a/app/config/config.toml.example +++ b/app/config/config.toml.example @@ -14,8 +14,8 @@ show_anime_collection = true # do you wish to show the manga collection? show_manga_collection = false -# cache driver for api calls (SQLDriver, RedisDriver) -cache_driver = SQLDriver +# cache driver for api calls (NullDriver, SQLDriver, RedisDriver) +cache_driver = NullDriver # path to public directory on the server asset_dir = "/../../public" \ No newline at end of file diff --git a/src/Aviat/Ion/Cache/CacheManager.php b/src/Aviat/Ion/Cache/CacheManager.php index a122df20..b5e32cb4 100644 --- a/src/Aviat/Ion/Cache/CacheManager.php +++ b/src/Aviat/Ion/Cache/CacheManager.php @@ -34,7 +34,7 @@ class CacheManager implements CacheInterface { if (empty($driverConf)) { - $driverConf = 'SQLDriver'; + $driverConf = 'NullDriver'; } $driverClass = __NAMESPACE__ . "\\Driver\\{$driverConf}"; diff --git a/src/Aviat/Ion/Cache/Driver/NullDriver.php b/src/Aviat/Ion/Cache/Driver/NullDriver.php new file mode 100644 index 00000000..b7d0bb9c --- /dev/null +++ b/src/Aviat/Ion/Cache/Driver/NullDriver.php @@ -0,0 +1,83 @@ +data = []; + } + + /** + * Retreive a value from the cache backend + * + * @param string $key + * @return mixed + */ + public function get($key) + { + return (array_key_exists($key, $this->data)) + ? $this->data[$key] + : NULL; + } + + /** + * Set a cached value + * + * @param string $key + * @param mixed $value + * @return CacheDriverInterface + */ + public function set($key, $value) + { + $this->data[$key] = $value; + return $this; + } + + /** + * Invalidate a cached value + * + * @param string $key + * @return CacheDriverInterface + */ + public function invalidate($key) + { + unset($this->data[$key]); + return $this; + } + + /** + * Clear the contents of the cache + * + * @return void + */ + public function invalidateAll() + { + $this->data = []; + } +} +// End of NullDriver.php \ No newline at end of file diff --git a/tests/Ion/Cache/Driver/NullDriverTest.php b/tests/Ion/Cache/Driver/NullDriverTest.php new file mode 100644 index 00000000..6771fdea --- /dev/null +++ b/tests/Ion/Cache/Driver/NullDriverTest.php @@ -0,0 +1,17 @@ +driver = new NullDriver($this->container); + } +} \ No newline at end of file