From 8e83e404fbb1d0dc770e5b41fa315a674541459a Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 21 Aug 2016 18:46:34 -0400 Subject: Add FileCache driver --- app/Core/Cache/Base.php | 38 --------------- app/Core/Cache/BaseCache.php | 71 ++++++++++++++++++++++++++++ app/Core/Cache/CacheInterface.php | 45 ------------------ app/Core/Cache/FileCache.php | 98 +++++++++++++++++++++++++++++++++++++++ app/Core/Cache/MemoryCache.php | 10 ++-- 5 files changed, 174 insertions(+), 88 deletions(-) delete mode 100644 app/Core/Cache/Base.php create mode 100644 app/Core/Cache/BaseCache.php delete mode 100644 app/Core/Cache/CacheInterface.php create mode 100644 app/Core/Cache/FileCache.php (limited to 'app/Core/Cache') diff --git a/app/Core/Cache/Base.php b/app/Core/Cache/Base.php deleted file mode 100644 index d62b8507..00000000 --- a/app/Core/Cache/Base.php +++ /dev/null @@ -1,38 +0,0 @@ -get($key); - - if ($result === null) { - $result = call_user_func_array(array($class, $method), array_splice($args, 1)); - $this->set($key, $result); - } - - return $result; - } -} diff --git a/app/Core/Cache/BaseCache.php b/app/Core/Cache/BaseCache.php new file mode 100644 index 00000000..04f8d220 --- /dev/null +++ b/app/Core/Cache/BaseCache.php @@ -0,0 +1,71 @@ +get($key); + + if ($result === null) { + $result = call_user_func_array(array($class, $method), array_splice($args, 1)); + $this->set($key, $result); + } + + return $result; + } +} diff --git a/app/Core/Cache/CacheInterface.php b/app/Core/Cache/CacheInterface.php deleted file mode 100644 index d9e9747a..00000000 --- a/app/Core/Cache/CacheInterface.php +++ /dev/null @@ -1,45 +0,0 @@ -createCacheFolder(); + file_put_contents($this->getFilenameFromKey($key), serialize($value)); + } + + /** + * Retrieve an item from the cache by key + * + * @access public + * @param string $key + * @return mixed Null when not found, cached value otherwise + */ + public function get($key) + { + $filename = $this->getFilenameFromKey($key); + + if (file_exists($filename)) { + return unserialize(file_get_contents($filename)); + } + + return null; + } + + /** + * Remove all items from the cache + * + * @access public + */ + public function flush() + { + $this->createCacheFolder(); + Tool::removeAllFiles(CACHE_DIR, false); + } + + /** + * Remove an item from the cache + * + * @access public + * @param string $key + */ + public function remove($key) + { + $filename = $this->getFilenameFromKey($key); + + if (file_exists($filename)) { + unlink($filename); + } + } + + /** + * Get absolute filename from the key + * + * @access protected + * @param string $key + * @return string + */ + protected function getFilenameFromKey($key) + { + return CACHE_DIR.DIRECTORY_SEPARATOR.$key; + } + + /** + * Create cache folder if missing + * + * @access protected + * @throws LogicException + */ + protected function createCacheFolder() + { + if (! is_dir(CACHE_DIR)) { + if (! mkdir(CACHE_DIR, 0755)) { + throw new LogicException('Unable to create cache directory: '.CACHE_DIR); + } + } + } +} diff --git a/app/Core/Cache/MemoryCache.php b/app/Core/Cache/MemoryCache.php index 39e3947b..4fb94728 100644 --- a/app/Core/Cache/MemoryCache.php +++ b/app/Core/Cache/MemoryCache.php @@ -3,12 +3,12 @@ namespace Kanboard\Core\Cache; /** - * Memory Cache + * Memory Cache Driver * - * @package cache + * @package Kanboard\Core\Cache * @author Frederic Guillot */ -class MemoryCache extends Base implements CacheInterface +class MemoryCache extends BaseCache { /** * Container @@ -19,7 +19,7 @@ class MemoryCache extends Base implements CacheInterface private $storage = array(); /** - * Save a new value in the cache + * Store an item in the cache * * @access public * @param string $key @@ -31,7 +31,7 @@ class MemoryCache extends Base implements CacheInterface } /** - * Fetch value from cache + * Retrieve an item from the cache by key * * @access public * @param string $key -- cgit v1.2.3