Simple and practical php caching function
Author:Eve Cole
Update Time:2009-06-06 18:17:45
/**
* @Description: File cache output
* @Parameter: $cachefile => cache file (absolute path)
* @Parameter: $pertime => Interval time for cached output
* @parameter: $sql => sql statement
* @Parameter: $templatefile => Template file name (absolute path)
**/
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';
}
}