From 72d4a9cd3e4bdaf0761ab415ca7ff08eee99dbc4 Mon Sep 17 00:00:00 2001 From: Alex Soncodi Date: Mon, 28 Oct 2013 23:38:50 -0500 Subject: [PATCH 1/3] Add disk cache. --- src/Handlebars/Cache/Disk.php | 110 ++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/Handlebars/Cache/Disk.php diff --git a/src/Handlebars/Cache/Disk.php b/src/Handlebars/Cache/Disk.php new file mode 100644 index 0000000..0f1fea9 --- /dev/null +++ b/src/Handlebars/Cache/Disk.php @@ -0,0 +1,110 @@ + + * @copyright 2013 (c) Brokerloop, Inc. + * @license MIT + * @version GIT: $Id$ + * @link http://xamin.ir + */ + + +/** + * A flat-file filesystem cache. + * + * @category Xamin + * @package Handlebars + * @author Alex Soncodi + * @copyright 2013 (c) Brokerloop, Inc. + * @license MIT + * @version Release: @package_version@ + * @link http://xamin.ir + */ + +class Handlebars_Cache_Disk implements Handlebars_Cache +{ + private $_path = ''; + private $_prefix = ''; + private $_suffix = ''; + + /** + * Construct the disk cache. + * + * @param string $path Filesystem path to the disk cache location + * @param string $prefix optional file prefix, defaults to empty string + * @param string $suffix optional file extension, defaults to empty string + */ + public function __construct($path, $prefix = '', $suffix = '') + { + if (empty($path)) { + throw new InvalidArgumentException('Must specify disk cache path'); + } + + $this->_path = $path; + $this->_prefix = $prefix; + $this->_suffix = $suffix; + } + + /** + * Gets the full disk path for a given cache item's file, + * taking into account the cache path, optional prefix, + * and optional extension. + * + * @param string $name Name of the cache item + */ + private function getPath($name) + { + return $this->_path . DIRECTORY_SEPARATOR . + $this->_prefix . $name . $this->_suffix; + } + + /** + * Get cache for $name if it exists. + * + * @param string $name Cache id + * + * @return data on hit, boolean false on cache not found + */ + public function get($name) + { + $path = $this->getPath($name); + + return (file_exists($path)) ? + unserialize(@file_get_contents($path)) : false; + } + + /** + * Set a cache + * + * @param string $name cache id + * @param mixed $value data to store + * + * @return void + */ + public function set($name, $value) + { + $path = $this->getPath($name); + + @file_put_contents($path, serialize($value)); + } + + /** + * Remove cache + * + * @param string $name Cache id + * + * @return void + */ + public function remove($name) + { + $path = $this->getPath($name); + + @unlink($path); + } +} From 1b65a63b0c21f5cfa0ae677c221d5c0dd19c7770 Mon Sep 17 00:00:00 2001 From: Alex Soncodi Date: Tue, 29 Oct 2013 00:13:58 -0500 Subject: [PATCH 2/3] Create cache dir if it does not exist. --- src/Handlebars/Cache/Disk.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Handlebars/Cache/Disk.php b/src/Handlebars/Cache/Disk.php index 0f1fea9..7915422 100644 --- a/src/Handlebars/Cache/Disk.php +++ b/src/Handlebars/Cache/Disk.php @@ -45,6 +45,13 @@ class Handlebars_Cache_Disk implements Handlebars_Cache if (empty($path)) { throw new InvalidArgumentException('Must specify disk cache path'); } + else if (!is_dir($path)) { + @mkdir($path, 0777, true); + + if (!is_dir($path)) { + throw new RuntimeException('Could not create cache file path'); + } + } $this->_path = $path; $this->_prefix = $prefix; From a7a01ed2ffb688722d8420fdb64690c3c1377573 Mon Sep 17 00:00:00 2001 From: Alex Soncodi Date: Tue, 29 Oct 2013 14:52:13 -0500 Subject: [PATCH 3/3] Do not ignore I/O errors. --- src/Handlebars/Cache/Disk.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Handlebars/Cache/Disk.php b/src/Handlebars/Cache/Disk.php index 7915422..1ded275 100644 --- a/src/Handlebars/Cache/Disk.php +++ b/src/Handlebars/Cache/Disk.php @@ -83,7 +83,7 @@ class Handlebars_Cache_Disk implements Handlebars_Cache $path = $this->getPath($name); return (file_exists($path)) ? - unserialize(@file_get_contents($path)) : false; + unserialize(file_get_contents($path)) : false; } /** @@ -98,7 +98,7 @@ class Handlebars_Cache_Disk implements Handlebars_Cache { $path = $this->getPath($name); - @file_put_contents($path, serialize($value)); + file_put_contents($path, serialize($value)); } /** @@ -112,6 +112,6 @@ class Handlebars_Cache_Disk implements Handlebars_Cache { $path = $this->getPath($name); - @unlink($path); + unlink($path); } }