fsLocation = rtrim($location, "/ \t\n\r\0\x0B");
/*
* Just update - don't freak out if the size isn't right, as the user might
* update it to non-default, they might not do anything with the cache,
* they might clean it themselves, etc. give them time. We'll freak when we
* add.
*/
foreach(glob($this->fsLocation."/*".self::$fileExt) as $file) {
self::addToSize(filesize($file));
}
}
/**
* Generates the full filename/path that would be used for a given URL's
* cache object.
*
* @param string $url The URL for the cached item.
* @return string
*/
public function makeFilename($url) {
return "$this->fsLocation/".self::makeKey($url).self::$fileExt;
}
public function set($url, &$item) {
if(empty($url)) {
throw new SagException('You need to provide a URL to cache.');
}
if(!parent::mayCache($item)) {
return false;
}
$serialized = json_encode($item);
$target = self::makeFilename($url);
// If it already exists, then remove the old version but keep a copy
if(is_file($target)) {
$oldCopy = self::get($url);
self::remove($url);
}
$fh = fopen($target, "w"); //in case self::remove() didn't get it?
fwrite($fh, $serialized, strlen($serialized)); //don't throw up if we fail - we're not mission critical
self::addToSize(filesize($target));
fclose($fh);
// Only return the $oldCopy if it exists
return (isset($oldCopy) && is_object($oldCopy)) ? $oldCopy : true;
}
public function get($url) {
$target = self::makeFilename($url);
if(!is_file($target)) {
return null;
}
if(!is_readable($target)) {
throw new SagException("Could not read the cache file for $url at $target - please check its permissions.");
}
return json_decode(file_get_contents($target));
}
public function remove($url) {
$target = $this->makeFilename($url);
return self::removeFile($target);
}
public function clear() {
$part = false;
foreach(glob($this->fsLocation."/*".self::$fileExt) as $file) {
if(!self::removeFile($file)) {
$part = true;
}
}
return !$part;
}
private function removeFile($path) {
$size = filesize($path);
if(!unlink($path)) {
return false;
}
self::addToSize(-$size);
return true;
}
}
?>