這是是在原版的技術上增強版的緩存插件,主要緩存css,js,圖片。
沒有緩存頁面,這個有空再加上吧。
如今的頁面通常採用Div+Css的形式,頁面不大,css和Js佔了很大的比重,所以把這部分壓縮傳送就能夠大大加快頁面的打開速度,如今99%以上的瀏覽器支持壓縮,因此爲這個提供了可行性。
說明:
1,在服務器緩存了壓縮過的文件,再次訪問減小再壓縮時間,下降CPU佔用率。
2,經過設置客戶端文件緩存時間,下降再次請求次數,可下降85%以上。
3,圖片由於已是壓縮格式,只是設置客戶端緩存時間,不作壓縮處理。
使用方法:
1,服務器必須支持gzip,Rewrite功能。
2,在。htacess文件的「RewriteBase /」下面一行添加
RewriteRule (.*.css$|.*.js$|.*.jpg$|.*.gif$|.*.png$) gzip.php?$1 [L]
3,上傳gzip.php到根目錄
4,在根目錄建cache文件夾,保證可讀寫。
下面是gzip.php,你們把源代碼給保存爲gzip.php,放到網站根目錄就能夠了。
<?php
// <!-- 公共的返回header的子程序 -->
function sendheader($last_modified, $p_type, $content_length = 0) {
// 設置客戶端緩存有效時間
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 15360000) . "GMT");
header("Cache-Control: max-age=315360000");
header("Pragma: ");
// 設置最後修改時間
header("Last-Modified: " . $last_modified);
// 設置文件類型信息
header($p_type);
header("Content-Length: " . $content_length);
}
define('ABSPATH', dirname(__FILE__) . '/');
$cache = true;
$cachedir = 'cache/'; //存放gz文件的目錄,確保可寫
if (emptyempty($_SERVER['QUERY_STRING'])) exit();
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
if (emptyempty($gzip)) $cache = false;
$key = array_shift(explode('?', $_SERVER['QUERY_STRING']));
$key = str_replace('../', '', $key);
$filename = ABSPATH . $key;
$symbol = '_';
$rel_path = str_replace(ABSPATH, '', dirname($filename));
$namespace = str_replace('/', $symbol, $rel_path);
$cache_filename = ABSPATH . $cachedir . $namespace . $symbol . basename($filename) . '.gz'; //生成gz文件路徑
$ext = array_pop(explode('.', $filename)); //根據後綴判斷文件類型信息
$type = "Content-type: text/html"; //默認的文件類型
switch ($ext) {
case 'css':
$type = "Content-type: text/css";
break;
case 'js':
$type = "Content-type: text/javascript";
break;
case 'gif':
$cache = false;
$type = "Content-type: p_w_picpath/gif";
break;
case 'jpg':
$cache = false;
$type = "Content-type: p_w_picpath/jpeg";
break;
case 'png':
$cache = false;
$type = "Content-type: p_w_picpath/png";
break;
default:
exit();
}
if ($cache) {
if (file_exists($cache_filename)) { // 假如存在gz文件
$mtime = filemtime($cache_filename);
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
) {
// 與瀏覽器cache中的文件修改日期一致,返回304
header ("HTTP/1.1 304 Not Modified");
// 發送客戶端header
header("Content-Encoding :gzip");
sendheader($gmt_mtime, $type);
} else {
// 讀取gz文件輸出
$content = file_get_contents($cache_filename);
// 發送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding: gzip");
// 發送數據
echo $content;
}
} else if (file_exists($filename)) { // 沒有對應的gz文件
$mtime = mktime();
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
// 讀取文件
$content = file_get_contents($filename);
// 去掉空白的部分
// $content = ltrim($content);
// 壓縮文件內容
$content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
// 發送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding: gzip");
// 發送數據
echo $content;
// 寫入文件
file_put_contents($cache_filename, $content);
} else {
header("HTTP/1.0 404 Not Found");
}
} else { // 處理不使用Gzip模式下的輸出。原理基本同上
if (file_exists($filename)) {
$mtime = filemtime($filename);
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
) {
// 與瀏覽器cache中的文件修改日期一致,返回304
header ("HTTP/1.1 304 Not Modified");
// 發送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
header("Content-Encoding :gzip");
} else {
// 讀取文件輸出
$content = file_get_contents($filename);
// 發送客戶端header
sendheader($gmt_mtime, $type, strlen($content));
// 發送數據
echo $content;
}
} else {
header("HTTP/1.0 404 Not Found");
}
}
?>