Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to cache any objects and arrays, not only strings #17

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
42 changes: 23 additions & 19 deletions Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,22 @@ 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)
{
$path = array();

// Getting the length of the filename before the extension
$parts = explode('.', $filename);
$len = strlen($parts[0]);
$hashedFilename = md5($filename);

for ($i=0; $i<min($len, $this->prefixSize); $i++) {
$path[] = $filename[$i];
for ($i=0; $i<$this->prefixSize; $i++) {
$path[] = $hashedFilename[$i];

}
$path = implode('/', $path);
Expand All @@ -151,7 +152,7 @@ public function getCacheFile($filename, $actual = false, $mkdir = false)
mkdir($actualDir, $this->directoryMode, true);
}

$path .= '/' . $filename;
$path .= '/' . $hashedFilename;

if ($actual) {
return $this->getActualCacheDirectory() . '/' . $path;
Expand All @@ -163,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())
{
Expand Down Expand Up @@ -239,7 +240,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;
}
Expand All @@ -258,7 +259,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;
}
Expand All @@ -279,11 +280,14 @@ protected function isRemote($file)
/**
* Get or create the cache entry
*
* @param $filename the cache file name
* @param $conditions an array of conditions about expiration
* @param $function the closure to call if the file does not exists
* @param $file returns the cache file or the file contents
* @param $actual returns the actual cache file
* @param $filename String the cache file name
* @param $conditions[] an array of conditions about expiration
* @param $function Callable the closure to call if the file does not exists
* @param $file Boolean returns the cache file or the file contents
* @param $actual Boolean returns the actual cache file
*
* @return mixed
* @throws InvalidArgumentException
*/
public function getOrCreate($filename, array $conditions = array(), $function, $file = false, $actual = false)
{
Expand All @@ -295,7 +299,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);
Expand All @@ -307,7 +311,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));
}
}

Expand Down