php 页面缓存的做法,使用php进行页面缓存

PHP以输出缓冲的形式提供了一种非常简单的动态缓存解决方案.如果在最近5分钟内缓存了该站点的首页(生成最多流量),则现在可以从缓存副本提供.

$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 "n";

exit;

}

ob_start(); // start the output buffer

?>

.. Your usual PHP script and HTML here ...

// 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();

?>

这是一个简单的缓存类型,

你可以在这里看到它

你可以使用Smarty有缓存技术