class PageCache {
/**
* @var string $file 緩存文件地址
* @access public
*/
public $file;
/**
* @var int $cacheTime 緩存時間
* @access public
*/
public $cacheTime = 3600;
/**
* 構造函數
* @param string $file 緩存文件地址
* @param int $cacheTime 緩存時間
*/
function __construct($file, $cacheTime = 3600) {
$this->file = $file;
$this->cacheTime = $cacheTime;
}
/**
* 取緩存內容
* @param bool 是否直接輸出,true直接轉到緩存頁,false返回緩存內容
* @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;
}
}
/**
* 設置緩存內容
* @param $content 內容html字符串
*/
public function set($content) {
$fp = fopen($this->file, 'w');
fwrite($fp, $content);
fclose($fp);
}
}