Skip to content

Commit

Permalink
Bring config keys usecache and storecache usage into the cache class
Browse files Browse the repository at this point in the history
  • Loading branch information
tboothman committed May 3, 2015
1 parent 8c95541 commit 0021fd0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
18 changes: 13 additions & 5 deletions imdb_cache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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!");
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions imdb_page.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/imdbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 30 additions & 4 deletions tests/imdb_cacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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');
Expand All @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/test.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
imdbsite = "test.local"
cachedir = /somefolder
storecache = false
usecache = false

0 comments on commit 0021fd0

Please sign in to comment.