[英]How can I cache a dynamic page to a flat file in PHP?
我正在尝试推出一个缓存系统以减少数据库的负载。
我希望能够比较平面文件最近一次更新的时间戳,以确保该文件最近一次更新不久。
这是代码:
$cache_file = $_GET[ 'page_id' ] . '.html';
function cache() {
// Here i want to get the timestamp of the file that was previously cached,
// if it exists.
// If the file doesn't exist, create it.
// If it does exist, check last modified time, if it's too long ago, then overwrite
// the file.
$ob = ob_get_contents();
file_put_contents( $cache_file, $ob );
}
function loadFromCache( $page_id ) {
$file_name = $page_id . '.html';
if( ! file_exists( $file_name ) ) {
return false;
}
readfile( $file_name );
return true;
}
谢谢。
您可以使用filemtime()
来获取文件的修改时间。
如果要获取当前文件的最后修改的时间戳,可以使用以下代码:
<?php
$stat = (stat(__FILE__));
echo 'Document last updated: ', date('M d Y', $stat['mtime']);
?>
为什么不使用的PHP cacheing一个选项在那里 ? PHP加速器还是PHP:APC? 这些可为您提供所需的现成解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.