From 0021fd0ce68ad951b4f50d53dde2ff70771e9551 Mon Sep 17 00:00:00 2001 From: Tom Boothman Date: Sun, 3 May 2015 13:24:43 +0100 Subject: [PATCH] Bring config keys usecache and storecache usage into the cache class --- imdb_cache.class.php | 18 +++++++++++++----- imdb_page.class.php | 8 -------- tests/imdbTest.php | 2 ++ tests/imdb_cacheTest.php | 34 ++++++++++++++++++++++++++++++---- tests/resources/test.ini | 2 ++ 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/imdb_cache.class.php b/imdb_cache.class.php index b14252ea..bb916310 100644 --- a/imdb_cache.class.php +++ b/imdb_cache.class.php @@ -6,7 +6,7 @@ * File caching * Caches files to disk in config->cachedir optionally gzipping if config->usezip * - * Config keys used: cachedir cache_expire usezip converttozip + * Config keys used: cachedir cache_expire usezip converttozip usecache storecache */ class imdb_cache { @@ -24,11 +24,11 @@ public function __construct(mdb_config $config, imdb_logger $logger) { $this->config = $config; $this->logger = $logger; - if (!is_dir($this->config->cachedir)) { + if (($this->config->usecache || $this->config->storecache) && !is_dir($this->config->cachedir)) { $this->logger->critical("[Cache] Configured cache directory [{$this->config->cachedir}] does not exist!"); throw new imdb_exception("[Cache] Configured cache directory [{$this->config->cachedir}] does not exist!"); } - if (!is_writable($this->config->cachedir)) { + if ($this->config->storecache && !is_writable($this->config->cachedir)) { $this->logger->critical("[Cache] Configured cache directory [{$this->config->cachedir}] lacks write permission!"); throw new imdb_exception("[Cache] Configured cache directory [{$this->config->cachedir}] lacks write permission!"); } @@ -37,15 +37,19 @@ public function __construct(mdb_config $config, imdb_logger $logger) { /** * Get string value of $key from cache * @param string $key - * @return string|boolean false on failure / cache miss + * @return string|null null on failure / cache miss */ public function get($key) { + if (!$this->config->usecache) { + return null; + } + $cleanKey = $this->sanitiseKey($key); $fname = $this->config->cachedir . '/' . $cleanKey; if (!file_exists($fname)) { $this->logger->debug("[Cache] Cache miss for [$key]"); - return false; + return null; } $this->logger->debug("[Cache] Cache hit for [$key]"); if ($this->config->usezip) { @@ -75,6 +79,10 @@ public function get($key) { * @return bool successful? */ public function set($key, $value) { + if (!$this->config->storecache) { + return false; + } + $cleanKey = $this->sanitiseKey($key); $fname = $this->config->cachedir . '/' . $cleanKey; diff --git a/imdb_page.class.php b/imdb_page.class.php index eacae8af..5c16c2dc 100644 --- a/imdb_page.class.php +++ b/imdb_page.class.php @@ -85,18 +85,10 @@ protected function requestPage($url) { } protected function getFromCache() { - if (!$this->config->usecache) { - return ''; - } - return $this->cache->get($this->getCacheKey()); } protected function saveToCache() { - if (!$this->config->storecache) { - return; - } - $this->cache->set($this->getCacheKey(), $this->pageString); } diff --git a/tests/imdbTest.php b/tests/imdbTest.php index 431ac22e..3b209dcf 100644 --- a/tests/imdbTest.php +++ b/tests/imdbTest.php @@ -27,6 +27,8 @@ public function testConstruct_from_ini_constructed_config() { $imdb = new imdb('0133093', $config); $this->assertEquals('test.local', $imdb->imdbsite); $this->assertEquals('/somefolder', $imdb->cachedir); + $this->assertEquals(false, $imdb->storecache); + $this->assertEquals(false, $imdb->usecache); } // @TODO tests for other types diff --git a/tests/imdb_cacheTest.php b/tests/imdb_cacheTest.php index dc4a0018..5cfac11a 100644 --- a/tests/imdb_cacheTest.php +++ b/tests/imdb_cacheTest.php @@ -5,6 +5,13 @@ require_once dirname(__FILE__) . '/../mdb_config.class.php'; class imdb_cacheTest extends PHPUnit_Framework_TestCase { + + private function getConfig() { + $config = new mdb_config(); + $config->cachedir = realpath(dirname(__FILE__).'/cache') . '/'; + return $config; + } + /** * @expectedException imdb_exception */ @@ -25,10 +32,30 @@ public function test_configured_directory_non_writeable_causes_exception() { new imdb_cache($config, new imdb_logger()); } + public function test_get_returns_null_when_usecache_is_false() { + $config = $this->getConfig(); + $cache = new imdb_cache($config, new imdb_logger()); + + $cache->set('usecacheTest', 'value'); + + $this->assertEquals('value', $cache->get('usecacheTest')); + $config->usecache = false; + $this->assertEquals(null, $cache->get('usecacheTest')); + } + + public function test_set_does_not_cache_when_storecache_is_false() { + $config = $this->getConfig(); + $config->storecache = false; + $cache = new imdb_cache($config, new imdb_logger()); + + $cache->set('storecacheTest', 'value'); + + $this->assertEquals(null, $cache->get('storecacheTest')); + } + public function test_set_get_zipped() { - $config = new mdb_config(); + $config = $this->getConfig(); $config->usezip = true; - $config->cachedir = realpath(dirname(__FILE__).'/cache') . '/'; $cache = new imdb_cache($config, new imdb_logger()); $setOk = $cache->set('test1', 'a value'); @@ -39,9 +66,8 @@ public function test_set_get_zipped() { } public function test_set_get_notzipped() { - $config = new mdb_config(); + $config = $this->getConfig(); $config->usezip = false; - $config->cachedir = realpath(dirname(__FILE__).'/cache') . '/'; $cache = new imdb_cache($config, new imdb_logger()); $setOk = $cache->set('test2', 'a value'); diff --git a/tests/resources/test.ini b/tests/resources/test.ini index 6e4ac984..7a52e01c 100644 --- a/tests/resources/test.ini +++ b/tests/resources/test.ini @@ -1,2 +1,4 @@ imdbsite = "test.local" cachedir = /somefolder +storecache = false +usecache = false