簡單實用的php快取函數
作者:Eve Cole
更新時間:2009-06-06 18:17:45
/**
* @說明: 文件快取輸出
* @參數: $cachefile => cache檔(絕對路徑)
* @參數: $pertime => 快取輸出的間隔時間
* @參數: $sql => sql語句
* @參數: $templatefile => 範本檔案名稱(絕對路徑)
**/
function __cache($cachefile,$pertime,$sql,$templatefile) {
global $db;
if(time() - @filemtime($cachefile) >= $pertime) {
$query = $db->query($sql);
while($r=$db->fetch($query)) {
$cachelist[] = $r;
}
include $templatefile.'.php';
$cacheserialize = serialize($cachelist);
file_put_contents($cachefile,$cacheserialize);
}else{
$cachelist = unserialize(file_get_contents($cachefile));
include $templatefile.'.php';
}
}