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/FileCache.php | 98 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 app/Core/Cache/FileCache.php (limited to 'app/Core/Cache/FileCache.php') 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 @@ +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); + } + } + } +} -- cgit v1.2.3