[英]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.