使用以下代码:
<?php
// Define path and name of cached file
$cachefile = 'cache/'.date('M-d-Y').'.php';
// How long to keep cache file?
$cachetime = 18000;
// Is cache file still fresh? If so, serve it.
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
include($cachefile);
exit;
}
// if no file or too old, render and capture HTML page.
ob_start();
?>
<html>
Webpage content in here
</html>
<?php
// Save the cached content to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
// Send browser output
ob_end_flush();
?>
有没有办法使页面上的DIV标签免于缓存..我有一个可以完全缓存的页面,除了每天更新的价目表,并希望阻止缓存该 DIV
谢谢:)
如果你的意思是HTTP缓存,那么不,你不能这样做。 在HTTP缓存中,要么全有,要么全无。
如果您的意思是服务器端缓存生成成本高昂的页面,那么您可以使用 cron 作业将第一次尝试查看页面的结果缓存到光盘中,并将生成的页面作为普通静态页面提供给 Web 服务器。
您可以将更改div 的内容加载到 PHP 变量中并将其发送到缓存文件(基本上使用缓存文件作为带有参数的模板)。