class PageCache {
/**
* @var string $file cache file address
* @access public
*/
public $file;
/**
* @var int $cacheTime cache time
* @access public
*/
public $cacheTime = 3600;
/**
* Constructor
* @param string $file cache file address
* @param int $cacheTime cache time
*/
function __construct($file, $cacheTime = 3600) {
$this->file = $file;
$this->cacheTime = $cacheTime;
}
/**
* Get the cached content
* @param bool Whether to output directly, true to go directly to the cache page, false to return the cached content
* @return mixed
*/
public function get($output = true) {
if (is_file($this->file) && (time()-filemtime($this->file))<=$this->cacheTime && !$_GET['nocache']) {
if ($output) {
header('location:' . $this->file);
exit;
} else {
return file_get_contents($this->file);
}
} else {
return false;
}
}
/**
* Set cache content
* @param $content content html string
*/
public function set($content) {
$fp = fopen($this->file, 'w');
fwrite($fp, $content);
fclose($fp);
}
}