【发布时间】:2015-01-14 10:08:04
【问题描述】:
当这个示例网页加载时,它会检查 /cache 文件夹中的缓存文件。如果没有缓存文件,它会调用 ob_start() 并创建一个 .html 缓存文件。问题是这个缓存过程每次都在工作,即使我没有调用 ob_start()。请给我提意见。谢谢。
// class file
function check_cache($dynamic_url) {
$cache_file = $this->cache_folder.md5($dynamic_url).$this->cache_ext;
if ((file_exists($cache_file)) && (time() - $this->cache_time < filemtime($cache_file))) {
// ob_start('ob_gzhandler');
readfile($cache_file);
ob_end_flush();
exit();
}
else {
//ob_start('ob_gzhandler');
}
}
function create_cache($dynamic_url) {
$cache_file = $this->cache_folder.md5($dynamic_url).$this->cache_ext;
$fp = fopen($cache_file, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
}
我从
调用这个类$cache->check_cache(get_full_url());
<h1>Today is <?php echo date('Y-m-d h:i:s'); ?></h1>
$cache->create_cache(get_full_url());
【问题讨论】:
-
“但 ob_end_flush() 似乎有效”是什么意思?如果你调用了 ob_end_flush() 但没有调用 ob_start(),ob_end_flush() 什么也不做。
-
这就是我所期望的...如您所见,我没有调用 ob_start() 但它一直在缓存页面...