php自己實作file cache

回覆文章
tony
文章: 585
註冊時間: 2012-02-26, 07:04
聯繫:

php自己實作file cache

未閱讀文章 tony »

以下為自己製作五分鐘file cache程式

代碼: 選擇全部

$cache_file = '/path/to/file_name';
 
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 5 ))) {
 
   $file = file_get_contents($cache_file);
} else {
    $file = 'xxx';
   //若cache過期,則重新對mysql進行query,並存到$file
 
   file_put_contents($cache_file, $file, LOCK_EX);
}
參考文件:
1.filemtime
2.file_put_contents
3.5-minute file cache in PHP
回覆文章

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客