我希望从各位身上得到指导,了解网站页面缓存的相关知识...由于我在使用PHP,所以希望有人能够向我解释如何在PHP中实现页面缓存。
PHP提供了一种非常简单的动态缓存解决方案,即输出缓冲。如果在过去的5分钟内已经缓存了网站的首页(它生成的流量最多),则现在将从缓存副本中提供服务。
<?php
$cachefile = "cache/".$reqfilename.".html";
$cachetime = 5 * 60; // 5 minutes
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))."
-->n";
exit;
}
ob_start(); // start the output buffer
?>
.. Your usual PHP script and HTML here ...
<?php
// open the cache file for writing
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();
?>
我很惊讶到目前为止没有任何回复提到除了运行 PHP 的服务器之外的其他缓存可能性。
HTTP 中有很多功能可以允许代理和浏览器重用以前提供的内容,而无需引用原始来源。这些功能非常多,以至于我甚至不会在 S.O. 的回复中尝试回答这个问题。
请参阅此tutorial,了解该主题的良好介绍。
C.
这里有一个对您有用的链接,关于缓存的基础知识以及如何在php中应用它。
请记住,在大多数情况下,适当的缓存应该在之前应用(也就是说,请求甚至不会到达 PHP 脚本)。