0
通過在頁面頂部使用這一行代碼ob_start('ob_gzhandler');
,根據Chrome控制檯,php輸出大約爲11 kb。當我試圖用下面的代碼緩存輸出時,我發現緩存的文件保存了大約65kb。更大的輸出尺寸是否需要緩存?有什麼辦法可以進一步壓縮緩存的輸出嗎?我曾嘗試爲html壓縮添加一些htaccess規則,但我不認爲這有幫助。使用PHP輸出緩衝區壓縮緩存的輸出
$id = $_GET["id"];
$cachefile ="cache/p_{$id}.html";
if (file_exists($cachefile)) {
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
ob_start('ob_gzhandler');
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush();
@ RedGiant:或者保存已壓縮的文件,就像你建議的那樣。這一切都取決於你的需求。現在,當你有gziped保存的緩存文件時,你必須將編碼設置爲gzip,以便瀏覽器知道數據是什麼:'if(file_exists($ cachefile)){header('Content-Encoding:gzip'); ...'。 – 2014-11-05 07:53:54