From b8eb77fb1e9fa737750d4314a196bd1cc1268e10 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 20 May 2016 16:43:25 +0200 Subject: [PATCH] * add PSR-2 compatibility #1 --- src/Cache.php | 396 ++++++++++++++++++++--------------------- src/CacheElement.php | 184 +++++++++---------- test/FileCacheTest.php | 104 +++++------ 3 files changed, 342 insertions(+), 342 deletions(-) diff --git a/src/Cache.php b/src/Cache.php index cc2fd30..8edae33 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -3,212 +3,212 @@ class Cache { - private $_path; - private $_default_lifetime; - private $_default_refresh; - - /** @var CacheElement[] */ - private $_cache = []; - - /** - * Cache constructor. - * @param $path - * @param int $default_lifetime - * @param bool $default_refresh - */ - public function __construct($path, $default_lifetime = 60, $default_refresh = false) - { - $this->_path = $path; - $this->_default_lifetime = $default_lifetime; - $this->_default_refresh = $default_refresh; - } - - /** - * @param $key - * @return bool - */ - public function has($key) - { - if (!is_null($element = $this->getOrFetchCacheElement($key))) { - if ($element->isAlive()) { - return true; - }else { - $this->forget($key); - } + private $_path; + private $_default_lifetime; + private $_default_refresh; + + /** @var CacheElement[] */ + private $_cache = []; + + /** + * Cache constructor. + * @param $path + * @param int $default_lifetime + * @param bool $default_refresh + */ + public function __construct($path, $default_lifetime = 60, $default_refresh = false) + { + $this->_path = $path; + $this->_default_lifetime = $default_lifetime; + $this->_default_refresh = $default_refresh; } - return false; - } - - /** - * @param $key - * @param null $default_value - * @return null|string - */ - public function get($key, $default_value = null) - { - if (!is_null($element = $this->getOrFetchCacheElement($key))) { - if ($element->isAlive()) { - return $element->getValue(); - }else { - $this->forget($key); - } + + /** + * @param $key + * @return bool + */ + public function has($key) + { + if (!is_null($element = $this->getOrFetchCacheElement($key))) { + if ($element->isAlive()) { + return true; + } else { + $this->forget($key); + } + } + return false; } - return $default_value; - } - - /** - * @param $key - * @param $value - * @param int|null $lifetime - * @param bool|null $refresh - * @return $this - */ - public function set($key, $value, $lifetime = null, $refresh = null) - { - if (is_null($element = $this->getOrFetchCacheElement($key))) { - $this->_cache[$key] = $this->newCacheElement($key, $value, $lifetime, $refresh); - } else { - $element->update( - $value, - $lifetime ?: $element->getLifetime(), - $refresh ?: $this->_default_refresh - ); + + /** + * @param $key + * @param null $default_value + * @return null|string + */ + public function get($key, $default_value = null) + { + if (!is_null($element = $this->getOrFetchCacheElement($key))) { + if ($element->isAlive()) { + return $element->getValue(); + } else { + $this->forget($key); + } + } + return $default_value; } - return $this; - } - - /** - * @param $key - * @return $this - */ - public function forget($key) - { - if (!is_null($element = $this->getOrFetchCacheElement($key))) { - $element->removeFromFs(); - unset($this->_cache[$key]); + + /** + * @param $key + * @param $value + * @param int|null $lifetime + * @param bool|null $refresh + * @return $this + */ + public function set($key, $value, $lifetime = null, $refresh = null) + { + if (is_null($element = $this->getOrFetchCacheElement($key))) { + $this->_cache[$key] = $this->newCacheElement($key, $value, $lifetime, $refresh); + } else { + $element->update( + $value, + $lifetime ?: $element->getLifetime(), + $refresh ?: $this->_default_refresh + ); + } + return $this; + } + + /** + * @param $key + * @return $this + */ + public function forget($key) + { + if (!is_null($element = $this->getOrFetchCacheElement($key))) { + $element->removeFromFs(); + unset($this->_cache[$key]); + } + return $this; } - return $this; - } - - /** - * @param $key - * @param \Closure $call - * @param int|null $lifetime - * @param bool|null $refresh - * @return $this - */ - public function remember($key, \Closure $call, $lifetime = null, $refresh = null) - { - if (!$this->has($key)) { - $this->_cache[$key] = $this->newCacheElement($key, $call(), $lifetime, $refresh); + + /** + * @param $key + * @param \Closure $call + * @param int|null $lifetime + * @param bool|null $refresh + * @return $this + */ + public function remember($key, \Closure $call, $lifetime = null, $refresh = null) + { + if (!$this->has($key)) { + $this->_cache[$key] = $this->newCacheElement($key, $call(), $lifetime, $refresh); + } + return $this; } - return $this; - } - - /** - * @param bool $keep_files - * @return $this - */ - public function flush($keep_files = false) - { - if (!$keep_files) { - array_map("unlink", glob($this->path() . DIRECTORY_SEPARATOR . "cache_*.json")); + + /** + * @param bool $keep_files + * @return $this + */ + public function flush($keep_files = false) + { + if (!$keep_files) { + array_map("unlink", glob($this->path() . DIRECTORY_SEPARATOR . "cache_*.json")); + } + $this->_cache = []; + return $this; } - $this->_cache = []; - return $this; - } - - /** - * @return $this - */ - public function writeCache() - { - foreach ($this->_cache as $element) { - $element->writeToFs(); + + /** + * @return $this + */ + public function writeCache() + { + foreach ($this->_cache as $element) { + $element->writeToFs(); + } + return $this; } - return $this; - } - - /** - * @return string - */ - private function path() - { - return rtrim($this->_path, DIRECTORY_SEPARATOR); - } - - /** - * @param $key - * @return string - */ - public function getPathForKey($key) - { - - return $this->path() . DIRECTORY_SEPARATOR . "cache_" . md5($key) . ".json"; - } - - /** - * @param $key - * @return CacheElement|null - */ - private function getOrFetchCacheElement($key) - { - $element = $this->getCacheElement($key); - if (is_null($element) && !is_null($element = $this->fetchCacheElement($key))) { - $this->_cache[$key] = $element; + + /** + * @return string + */ + private function path() + { + return rtrim($this->_path, DIRECTORY_SEPARATOR); } - return $element; - } - - /** - * @param $key - * @return CacheElement|null - */ - private function getCacheElement($key) - { - return array_key_exists($key, $this->_cache) ? $this->_cache[$key] : null; - } - - /** - * @param $key - * @return CacheElement|null - */ - private function fetchCacheElement($key) - { - $path = $this->getPathForKey($key); - if (is_file($path)) { - $cache = json_decode(file_get_contents($path), true); - $element = new CacheElement( - $this, - $key, - $cache["value"], - $cache["lifetime"], - $cache["start"], - $cache["refresh"] - ); - $this->_cache[$key] = $element; - return $element; + + /** + * @param $key + * @return string + */ + public function getPathForKey($key) + { + + return $this->path() . DIRECTORY_SEPARATOR . "cache_" . md5($key) . ".json"; + } + + /** + * @param $key + * @return CacheElement|null + */ + private function getOrFetchCacheElement($key) + { + $element = $this->getCacheElement($key); + if (is_null($element) && !is_null($element = $this->fetchCacheElement($key))) { + $this->_cache[$key] = $element; + } + return $element; + } + + /** + * @param $key + * @return CacheElement|null + */ + private function getCacheElement($key) + { + return array_key_exists($key, $this->_cache) ? $this->_cache[$key] : null; + } + + /** + * @param $key + * @return CacheElement|null + */ + private function fetchCacheElement($key) + { + $path = $this->getPathForKey($key); + if (is_file($path)) { + $cache = json_decode(file_get_contents($path), true); + $element = new CacheElement( + $this, + $key, + $cache["value"], + $cache["lifetime"], + $cache["start"], + $cache["refresh"] + ); + $this->_cache[$key] = $element; + return $element; + } + return null; + } + + /** + * @param $key + * @param $value + * @param int|null $lifetime + * @param int|null $refresh + * @return CacheElement + */ + private function newCacheElement($key, $value, $lifetime = null, $refresh = null) + { + return new CacheElement( + $this, + $key, + $value, + $lifetime ?: $this->_default_lifetime, + null, + $refresh ?: $this->_default_refresh + ); } - return null; - } - - /** - * @param $key - * @param $value - * @param int|null $lifetime - * @param int|null $refresh - * @return CacheElement - */ - private function newCacheElement($key, $value, $lifetime = null, $refresh = null) - { - return new CacheElement( - $this, - $key, - $value, - $lifetime ?: $this->_default_lifetime, - null, - $refresh ?: $this->_default_refresh - ); - } } \ No newline at end of file diff --git a/src/CacheElement.php b/src/CacheElement.php index 6227c8e..d5333cb 100644 --- a/src/CacheElement.php +++ b/src/CacheElement.php @@ -3,108 +3,108 @@ class CacheElement { - private $_cache_instance; - private $_key; - private $_value; - private $_lifetime; - private $_start; - private $_refresh; - private $_is_written; - private $_modified; + private $_cache_instance; + private $_key; + private $_value; + private $_lifetime; + private $_start; + private $_refresh; + private $_is_written; + private $_modified; - /** - * CacheElement constructor. - * @param $cache_instance - * @param $key - * @param $value - * @param int $lifetime - * @param int|null $start - * @param bool $refresh - */ - public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60, $start = null, $refresh = false) - { - $this->_cache_instance = $cache_instance; - $this->_key = (string) $key; - $this->_value = (string) $value; - $this->_lifetime = $lifetime; - $this->_start = $start ?: time(); - $this->_refresh = $refresh; - $this->_is_written = $start !== null; - $this->_modified = $start === null; - } + /** + * CacheElement constructor. + * @param $cache_instance + * @param $key + * @param $value + * @param int $lifetime + * @param int|null $start + * @param bool $refresh + */ + public function __construct(Cache $cache_instance, $key, $value, $lifetime = 60, $start = null, $refresh = false) + { + $this->_cache_instance = $cache_instance; + $this->_key = (string)$key; + $this->_value = (string)$value; + $this->_lifetime = $lifetime; + $this->_start = $start ?: time(); + $this->_refresh = $refresh; + $this->_is_written = $start !== null; + $this->_modified = $start === null; + } - /** - * @param $value - * @param int $lifetime - * @param bool $refresh - */ - public function update($value, $lifetime = 60, $refresh = false) - { - $this->_value = (string) $value; - $this->_lifetime = $lifetime; - $this->_start = time(); - $this->_refresh = $refresh; - $this->_modified = true; - } + /** + * @param $value + * @param int $lifetime + * @param bool $refresh + */ + public function update($value, $lifetime = 60, $refresh = false) + { + $this->_value = (string)$value; + $this->_lifetime = $lifetime; + $this->_start = time(); + $this->_refresh = $refresh; + $this->_modified = true; + } - /** - * @return string - */ - public function getValue() - { - if ($this->_refresh) { - $this->_start = time(); + /** + * @return string + */ + public function getValue() + { + if ($this->_refresh) { + $this->_start = time(); + } + return $this->_value; } - return $this->_value; - } - public function getLifeTime() - { - return $this->_lifetime; - } + public function getLifeTime() + { + return $this->_lifetime; + } - /** - * - */ - public function writeToFs() - { - if ($this->_modified) { - file_put_contents($this->path(), json_encode([ - "value" => $this->_value, - "lifetime" => $this->_lifetime, - "start" => $this->_start, - "refresh" => $this->_refresh, - ])); + /** + * + */ + public function writeToFs() + { + if ($this->_modified) { + file_put_contents($this->path(), json_encode([ + "value" => $this->_value, + "lifetime" => $this->_lifetime, + "start" => $this->_start, + "refresh" => $this->_refresh, + ])); + } + $this->_is_written = true; + $this->_modified = false; } - $this->_is_written = true; - $this->_modified = false; - } - /** - * @return bool - */ - public function removeFromFs() - { - if ($this->_is_written) { - return unlink($this->path()); + /** + * @return bool + */ + public function removeFromFs() + { + if ($this->_is_written) { + return unlink($this->path()); + } + return true; } - return true; - } - /** - * @return bool - */ - public function isAlive() - { - return (time() - $this->_start) <= $this->_lifetime; - } + /** + * @return bool + */ + public function isAlive() + { + return (time() - $this->_start) <= $this->_lifetime; + } - /** - * @return string - */ - private function path() - { - return $this->_cache_instance->getPathForKey($this->_key); - } + /** + * @return string + */ + private function path() + { + return $this->_cache_instance->getPathForKey($this->_key); + } } \ No newline at end of file diff --git a/test/FileCacheTest.php b/test/FileCacheTest.php index aece909..cf3a340 100644 --- a/test/FileCacheTest.php +++ b/test/FileCacheTest.php @@ -3,57 +3,57 @@ class FileCacheTest extends \PHPUnit_Framework_TestCase { - public function test() - { - $cache = new Cache(__DIR__."/test_path/"); - - // get if not set - $this->assertNull($cache->get("foo")); - $this->assertEquals("default", $cache->get("foo", "default")); - - // test has = false - $this->assertFalse($cache->has("foo")); - - // set & get if set - $cache->set("foo", "bar"); - $this->assertEquals("bar", $cache->get("foo")); - - // test has = true - $this->assertTrue($cache->has("foo")); - - //test if cache has been written - $cache->writeCache()->flush(true); - $this->assertEquals("bar", $cache->get("foo")); - - // test remove - $cache->forget("foo"); - $this->assertNull($cache->get("foo")); - - // test remember - $cache->remember("foo2", function () { - return "bar2"; - }); - $cache->remember("foo2", function () { - return "this will never be set"; - }); - $this->assertEquals("bar2", $cache->get("foo2")); - - // test timeout - $cache->set("foo3", "bar3", 1); - sleep(2); - $this->assertNull($cache->get("foo3")); - - // test refresh - $cache->set("foo4", "bar4", 4); - sleep(2); - $this->assertEquals("bar4", $cache->get("foo4")); - sleep(2); - $this->assertEquals("bar4", $cache->get("foo4")); - sleep(5); - $this->assertNull($cache->get("foo4")); - - // test flush - $cache->flush(); - } + public function test() + { + $cache = new Cache(__DIR__ . "/test_path/"); + + // get if not set + $this->assertNull($cache->get("foo")); + $this->assertEquals("default", $cache->get("foo", "default")); + + // test has = false + $this->assertFalse($cache->has("foo")); + + // set & get if set + $cache->set("foo", "bar"); + $this->assertEquals("bar", $cache->get("foo")); + + // test has = true + $this->assertTrue($cache->has("foo")); + + //test if cache has been written + $cache->writeCache()->flush(true); + $this->assertEquals("bar", $cache->get("foo")); + + // test remove + $cache->forget("foo"); + $this->assertNull($cache->get("foo")); + + // test remember + $cache->remember("foo2", function () { + return "bar2"; + }); + $cache->remember("foo2", function () { + return "this will never be set"; + }); + $this->assertEquals("bar2", $cache->get("foo2")); + + // test timeout + $cache->set("foo3", "bar3", 1); + sleep(2); + $this->assertNull($cache->get("foo3")); + + // test refresh + $cache->set("foo4", "bar4", 4); + sleep(2); + $this->assertEquals("bar4", $cache->get("foo4")); + sleep(2); + $this->assertEquals("bar4", $cache->get("foo4")); + sleep(5); + $this->assertNull($cache->get("foo4")); + + // test flush + $cache->flush(); + } } \ No newline at end of file