国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - PHP版網(wǎng)站緩存加快打開速度的方法分享

PHP版網(wǎng)站緩存加快打開速度的方法分享

2020-01-02 16:20PHP教程網(wǎng) PHP教程

PHP版網(wǎng)站緩存加快打開速度的方法分享,需要的朋友可以參考下

說明: 
1,在服務(wù)器緩存了壓縮過的文件,再次訪問減少再壓縮時(shí)間,降低CPU占用率。 
2,通過設(shè)置客戶端文件緩存時(shí)間,降低再次請求次數(shù),可降低85%以上。 
3,圖片因?yàn)橐呀?jīng)是壓縮格式,只是設(shè)置客戶端緩存時(shí)間,不做壓縮處理。 

使用方法: 
1,服務(wù)器必須支持gzip,Rewrite功能。 
2,在.htacess文件的“RewriteBase /”下面一行添加下面的代碼,見圖 
RewriteRule (.*.css$|.*.js$|.*.jpg$|.*.gif$|.*.png$) gzip.php?$1 [L] 
3,上傳gzip.php到根目錄 
4,在根目錄建cache文件夾,保證可讀寫。 

PHP版網(wǎng)站緩存加快打開速度的方法分享

復(fù)制代碼代碼如下:


<?php 
/** 
* @author Seraphim 
* @copyright 2012 
*/ 
// <!-- 公共的返回header的子程序 --> 
function sendheader($last_modified, $p_type, $content_length = 0) 

// 設(shè)置客戶端緩存有效時(shí)間 
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 15360000) . "GMT"); 
header("Cache-Control: max-age=315360000"); 
header("Pragma: "); 
// 設(shè)置最后修改時(shí)間 
header("Last-Modified: " . $last_modified); 
// 設(shè)置文件類型信息 
header($p_type); 
header("Content-Length: " . $content_length); 

define('ABSPATH', dirname(__file__) . '/'); 
$cache = true; 
$cachedir = 'cache/'; //存放gz文件的目錄,確保可寫 
if (empty($_SERVER['QUERY_STRING'])) 
exit(); 
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'); 
if (empty($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)); //根據(jù)后綴判斷文件類型信息 
$type = "Content-type: text/html"; //默認(rèn)的文件類型 
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: image/gif"; 
break; 
case 'jpg': 
$cache = false; 
$type = "Content-type: image/jpeg"; 
break; 
case 'png': 
$cache = false; 
$type = "Content-type: image/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"); 
// 發(fā)送客戶端header 
header("Content-Encoding :gzip"); 
sendheader($gmt_mtime, $type); 

else 

// 讀取gz文件輸出 
$content = file_get_contents($cache_filename); 
// 發(fā)送客戶端header 
sendheader($gmt_mtime, $type, strlen($content)); 
header("Content-Encoding: gzip"); 
// 發(fā)送數(shù)據(jù) 
echo $content; 


else 
if (file_exists($filename)) 
{ // 沒有對應(yīng)的gz文件 
$mtime = mktime(); 
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; 
// 讀取文件 
$content = file_get_contents($filename); 
// 去掉空白的部分 
// $content = ltrim($content); 
// 壓縮文件內(nèi)容 
$content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE); 
// 發(fā)送客戶端header 
sendheader($gmt_mtime, $type, strlen($content)); 
header("Content-Encoding: gzip"); 
// 發(fā)送數(shù)據(jù) 
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"); 
// 發(fā)送客戶端header 
sendheader($gmt_mtime, $type, strlen($content)); 
header("Content-Encoding :gzip"); 

else 

// 讀取文件輸出 
$content = file_get_contents($filename); 
// 發(fā)送客戶端header 
sendheader($gmt_mtime, $type, strlen($content)); 
// 發(fā)送數(shù)據(jù) 
echo $content; 


else 

header("HTTP/1.0 404 Not Found"); 


?> 

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲国产一区二区三区日本久久久 | 综合伊人 | baoyu123成人免费看视频 | 亚洲婷婷一区二区三区 | 午夜精品一区二区三区在线视频 | 精品国产一区二区 | 俺去俺来也在线www色官网 | 色二区 | 亚洲激情在线播放 | 精品伦精品一区二区三区视频 | av一区二区不卡 | 日日操夜夜操免费视频 | 无码一区二区三区视频 | 国产精品手机在线 | 国产精品久久久久久久久久免费 | 久久久久久国产免费 | 国产伦精品一区二区三区四区视频 | 日韩欧美亚洲精品 | 欧美亚洲国产一区 | 成人亚洲欧美 | 欧美成人一区二区 | 欧美日韩精品免费观看 | 亚洲一区二区在线播放 | 国产精品区一区二区三含羞草 | 欧美成人一区二区 | 91黄视频 | 国产精品日韩在线观看 | 亚洲视频在线播放 | 日本免费在线视频 | 日韩中文字幕在线视频 | 亚洲色图一区二区三区 | 精品久久久久久久久久久久 | 国产精品99久久久久久动医院 | 欧美日韩国产一区二区三区 | 久久久久久网址 | 夜夜视频 | 91精品国产综合久久婷婷香蕉 | 午夜激情在线 | 国产精品日韩一区 | 天天澡天天狠天天天做 | 日韩成人在线电影 |