时间:2021-07-01 10:21:17 帮助过:4人阅读
// load data with cache
function load_data($id,$cache_lifetime) {
// the return data
$data = array();
// make cache filename
$cache_filename = ‘cache_‘.$id.‘.php‘;
// check cache file‘s last modify time
$cache_filetime = filemtime($cache_filename);
if (time() - $cache_filetime <= $cache_lifetime) {
//** the cache is not expire
include($cache_filename);
} else {
//** the cache is expired
// load data from database
// ...
while ($dbo->nextRecord()) {
// $data[] = ...
}
// format the data as a php file
$data_cache = "
while (list($key, $val) = each($data)) {
$data_cache .= "$data[‘$key‘]=array(‘";
$data_cache .= "‘NAME‘=>"".qoute($val[‘NAME‘])."\","
$data_cache .= "‘VALUE‘=>\"".qoute($val[‘VALUE‘])."\""
$data_cache .= ";);\r\n";
}
$data_cache = "?>\r\n";
// save the data to the cache file
if ($fd = fopen($cache_filename,‘w+‘)) {
fputs($fd,$data_cache);
fclose($fd);
}
}
return $data;
}
?>
http://www.bkjia.com/PHPjc/317934.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/317934.htmlTechArticle数据缓存是web开发中常用的一种性能优化方法。目前主要文件缓存或者数据库缓存两种形式,数据库缓存数据库不是什么不可能的事情,的...