<?php
class Piwik_CacheFile
{
protected $cachePath;
protected $cachePrefix;
function __construct($directory)
{
$this->cachePath = PIWIK_USER_PATH . '/tmp/cache/' . $directory . '/';
}
function get($id)
{
$cache_complete = false;
$content = '';
// We are assuming that most of the time cache will exists
$ok = @include($this->cachePath . $id . '.php');
if ($ok && $cache_complete == true) {
return $content;
}
return false;
}
function set($id, $content)
{
if( !is_dir($this->cachePath))
{
Piwik_Common::mkdir($this->cachePath);
}
if (!is_writable($this->cachePath)) {
return false;
}
$id = $this->cachePath . $id . '.php';
$cache_literal = "<"."?php\n\n";
$cache_literal .= "$"."content = ".var_export($content, true).";\n\n";
$cache_literal .= "$"."cache_complete = true;\n\n";
$cache_literal .= "?".">";
// Write cache to a temp file, then rename it, overwritng the old cache
// On *nix systems this should guarantee atomicity
$tmp_filename = tempnam($this->cachePath, 'tmp_');
if ($fp = @fopen($tmp_filename, 'wb')) {
@fwrite ($fp, $cache_literal, strlen($cache_literal));
@fclose ($fp);
if (!@rename($tmp_filename, $id)) {
// On some systems rename() doesn't overwrite destination
@unlink($id);
if (!@rename($tmp_filename, $id)) {
// Make sure that no temporary file is left over
// if the destination is not writable
@unlink($tmp_filename);
}
}
return true;
}
return false;
}
function delete($id)
{
$filename = $this->cachePath . $id . '.php';
if (file_exists($filename)) {
@unlink ($filename);
return true;
}
return false;
}
}