From 67d7521bcda8a807bc1cb6ba5eba2c75d391c0ba Mon Sep 17 00:00:00 2001 From: Constantine Stanishevsky Date: Fri, 28 Nov 2014 20:12:04 +0100 Subject: [PATCH 1/4] Allow to cache any objects and arrays, not only strings --- Cache.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cache.php b/Cache.php index ef58580..305e72f 100644 --- a/Cache.php +++ b/Cache.php @@ -239,7 +239,7 @@ public function set($filename, $contents = '') { $cacheFile = $this->getCacheFile($filename, true, true); - file_put_contents($cacheFile, $contents); + file_put_contents($cacheFile, serialize($contents)); return $this; } @@ -258,7 +258,7 @@ public function write($filename, $contents = '') public function get($filename, array $conditions = array()) { if ($this->exists($filename, $conditions)) { - return file_get_contents($this->getCacheFile($filename, true)); + return unserialize(file_get_contents($this->getCacheFile($filename, true))); } else { return null; } @@ -295,7 +295,7 @@ public function getOrCreate($filename, array $conditions = array(), $function, $ $data = null; if ($this->check($filename, $conditions)) { - $data = file_get_contents($cacheFile); + $data = unserialize(file_get_contents($cacheFile)); } else { if(file_exists($cacheFile)) { unlink($cacheFile); @@ -307,7 +307,7 @@ public function getOrCreate($filename, array $conditions = array(), $function, $ if (!file_exists($cacheFile)) { $this->set($filename, $data); } else { - $data = file_get_contents($cacheFile); + $data = unserialize(file_get_contents($cacheFile)); } } From 54fb65e0a14f392461d4c7bf3e7b3491fd74dc57 Mon Sep 17 00:00:00 2001 From: Constantine Stanishevsky Date: Sat, 29 Nov 2014 01:42:05 +0100 Subject: [PATCH 2/4] Cache file name is encoded now, so we can use any chars as cache identifier --- Cache.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Cache.php b/Cache.php index 305e72f..2c74ee6 100644 --- a/Cache.php +++ b/Cache.php @@ -137,11 +137,10 @@ public function getCacheFile($filename, $actual = false, $mkdir = false) $path = array(); // Getting the length of the filename before the extension - $parts = explode('.', $filename); - $len = strlen($parts[0]); + $hashedFilename = md5($filename); - for ($i=0; $iprefixSize); $i++) { - $path[] = $filename[$i]; + for ($i=0; $i<$this->prefixSize; $i++) { + $path[] = $hashedFilename[$i]; } $path = implode('/', $path); @@ -151,7 +150,7 @@ public function getCacheFile($filename, $actual = false, $mkdir = false) mkdir($actualDir, $this->directoryMode, true); } - $path .= '/' . $filename; + $path .= '/' . $hashedFilename; if ($actual) { return $this->getActualCacheDirectory() . '/' . $path; From b1651fefaa713a9720bb16c0c7938b22a1de68e1 Mon Sep 17 00:00:00 2001 From: Constantine Stanishevsky Date: Sat, 29 Nov 2014 01:47:56 +0100 Subject: [PATCH 3/4] PHPDoc update --- Cache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cache.php b/Cache.php index 2c74ee6..8694088 100644 --- a/Cache.php +++ b/Cache.php @@ -162,8 +162,8 @@ public function getCacheFile($filename, $actual = false, $mkdir = false) /** * Checks that the cache conditions are respected * - * @param $cacheFile the cache file - * @param $conditions an array of conditions to check + * @param $cacheFile String the cache file + * @param $conditions[] an array of conditions to check */ protected function checkConditions($cacheFile, array $conditions = array()) { From b00daf69da6a183312a03b02f29bd7494548d452 Mon Sep 17 00:00:00 2001 From: Constantine Stanishevsky Date: Sat, 29 Nov 2014 01:47:56 +0100 Subject: [PATCH 4/4] PHPDoc update --- Cache.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Cache.php b/Cache.php index 2c74ee6..2e53c01 100644 --- a/Cache.php +++ b/Cache.php @@ -127,10 +127,12 @@ protected function mkdir($directory) /** * Gets the cache file name * - * @param $filename, the name of the cache file - * @param $actual get the actual file or the public file - * @param $mkdir, a boolean to enable/disable the construction of the + * @param $filename String, the name of the cache file + * @param $actual Boolean get the actual file or the public file + * @param $mkdir Boolean, a boolean to enable/disable the construction of the * cache file directory + * + * @return String */ public function getCacheFile($filename, $actual = false, $mkdir = false) { @@ -162,8 +164,8 @@ public function getCacheFile($filename, $actual = false, $mkdir = false) /** * Checks that the cache conditions are respected * - * @param $cacheFile the cache file - * @param $conditions an array of conditions to check + * @param $cacheFile String the cache file + * @param $conditions[] an array of conditions to check */ protected function checkConditions($cacheFile, array $conditions = array()) {