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();
?>
Run Code Online (Sandbox Code Playgroud)
这是一个简单的缓存类型,
你可以在这里看到它
http://www.theukwebdesigncompany.com/articles/php-caching.php
你可以使用Smarty有缓存技术
http://www.nusphere.com/php/templates_smarty_caching.htm
这是一个对您有用的链接,涉及缓存的基础知识以及如何将其应用于 php。
http://www.devshed.com/c/a/PHP/Output-Caching-with-PHP/
请记住,在大多数情况下,应尽早应用适当的缓存(即请求甚至不会到达 php 脚本)。