diff options
Diffstat (limited to 'app/Core/Cache')
-rw-r--r-- | app/Core/Cache/BaseCache.php (renamed from app/Core/Cache/Base.php) | 6 | ||||
-rw-r--r-- | app/Core/Cache/CacheInterface.php | 14 | ||||
-rw-r--r-- | app/Core/Cache/FileCache.php | 98 | ||||
-rw-r--r-- | app/Core/Cache/MemoryCache.php | 10 |
4 files changed, 113 insertions, 15 deletions
diff --git a/app/Core/Cache/Base.php b/app/Core/Cache/BaseCache.php index d62b8507..b51c4c0c 100644 --- a/app/Core/Cache/Base.php +++ b/app/Core/Cache/BaseCache.php @@ -3,12 +3,12 @@ namespace Kanboard\Core\Cache; /** - * Base class for cache drivers + * Base Class for Cache Drivers * - * @package cache + * @package Kanboard\Core\Cache * @author Frederic Guillot */ -abstract class Base +abstract class BaseCache implements CacheInterface { /** * Proxy cache diff --git a/app/Core/Cache/CacheInterface.php b/app/Core/Cache/CacheInterface.php index d9e9747a..19bd6ef7 100644 --- a/app/Core/Cache/CacheInterface.php +++ b/app/Core/Cache/CacheInterface.php @@ -3,15 +3,15 @@ namespace Kanboard\Core\Cache; /** - * Cache Interface + * Interface CacheInterface * - * @package cache - * @author Frederic Guillot + * @package Kanboard\Core\Cache + * @author Frederic Guillot */ interface CacheInterface { /** - * Save a new value in the cache + * Store an item in the cache * * @access public * @param string $key @@ -20,7 +20,7 @@ interface CacheInterface public function set($key, $value); /** - * Fetch value from cache + * Retrieve an item from the cache by key * * @access public * @param string $key @@ -29,14 +29,14 @@ interface CacheInterface public function get($key); /** - * Clear all cache + * Remove all items from the cache * * @access public */ public function flush(); /** - * Remove cached value + * Remove an item from the cache * * @access public * @param string $key diff --git a/app/Core/Cache/FileCache.php b/app/Core/Cache/FileCache.php new file mode 100644 index 00000000..d477a1f3 --- /dev/null +++ b/app/Core/Cache/FileCache.php @@ -0,0 +1,98 @@ +<?php + +namespace Kanboard\Core\Cache; + +use Kanboard\Core\Tool; +use LogicException; + +/** + * Class FileCache + * + * @package Kanboard\Core\Cache + */ +class FileCache extends BaseCache +{ + /** + * Store an item in the cache + * + * @access public + * @param string $key + * @param string $value + */ + public function set($key, $value) + { + $this->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 |