国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ahjesus 前端緩存原理 轉載

2019-11-17 01:42:31
字體:
來源:轉載
供稿:網友

ahjesus 前端緩存原理 轉載

LAMP緩存圖

從圖中我們可以看到網站緩存主要分為五部分

服務器緩存:主要是基于web反向代理的靜態服務器nginx和squid,還有apache2的mod_PRoxy和mod_cache模

瀏覽器緩存:包括頁面html緩存和圖片js,CSS等資源的緩存

php緩存:有很多免費的PHP緩沖加速工具,如apc eaccerlertor等

內存緩存:主要是采用memcache這種分布式緩存機制

數據庫緩存:通過配置數據庫緩存,以及數據存儲過程,連接池技術等

下面重點介紹瀏覽器緩存原理:

從上圖:我們可以知道瀏覽器緩存重要分為兩個部分:

頁面html的緩存圖片,css,js,Flash等緩存瀏覽器緩存是基于把頁面信息保存到用戶本地電腦硬盤里。

服務器緩存是基于把用戶訪問的頁面保存到服務器上的硬盤里。

頁面緩存的原理

頁面緩存狀態是由http header決定的,一個瀏覽器請求信息,一個是服務器響應信息。主要包括Pragma: no-cache、Cache-Control、 Expires、 Last-Modified、If-Modified-Since。其中Pragma: no-cache由HTTP/1.0規定,Cache-Control由HTTP/1.1規定。

從圖中我們可以看到原理主要分三步:

第一次請求:瀏覽器通過http的header報頭,附帶Expires,Cache-Control,Last-Modified/Etag向服務器請求,此時服務器記錄第一次請求的Last-Modified/Etag再次請求:

當瀏覽器再次請求的時候,附帶Expires,Cache-Control,If-Modified-Since/Etag向服務器請求

服務器根據第一次記錄的Last-Modified/Etag和再次請求的If-Modified-Since/Etag做對比,判斷是否需要更新,然后響應請求。

相關參數說明:

Cache-Control的主要參數Cache-Control: private/public Public 響應會被緩存,并且在多用戶間共享。 Private 響應只能夠作為私有的緩存,不能再用戶間共享。

Cache-Control: no-cache:不進行緩存

Cache-Control: max-age=x:緩存時間 以秒為單位

Cache-Control: must-revalidate:如果頁面是過期的 則去服務器進行獲取。

Expires:顯示的設置頁面過期時間

Last-Modified:請求對象最后一次的修改時間 用來判斷緩存是否過期 通常由服務器上文件的時間信息產生。

If-Modified-Since:客戶端發送請求附帶的信息 指瀏覽器緩存請求對象的最后修改日期 用來和服務器端的Last-Modified做比較。

Etag:ETag是一個可以 與Web資源關聯的記號(token),和Last-Modified功能才不多,也是一個標識符,一般和Last-Modified一起使用,加強服務器判斷的準確度。

轉載自 http://www.vicenteforever.com/2012/05/web-front-cache/

附代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web;using System.Configuration;namespace __System.Web{    public class MyHttpModule : IHttpModule    {        public void Dispose()        {        }        public void Init(Httpapplication context)        {            context.BeginRequest += new EventHandler(context_BeginRequest);            context.EndRequest += new EventHandler(context_EndRequest);        }        void context_EndRequest(object sender, EventArgs e)        {            ChangeCache(sender);        }        void context_BeginRequest(object sender, EventArgs e)        {            //HttpApplication application = (HttpApplication)sender;            //HttpContext context = application.Context;            //string filePath = context.Request.FilePath;            //string fileExtension = VirtualPathUtility.GetExtension(filePath);            //if (fileExtension.Equals(".html") || fileExtension.Equals(".htm"))            //{            //    context.Response.Write("<hr><h1><font color=red>" +            //        "HelloWorldModule: End of Request</font></h1>");            //}        }        void ChangeCache(object sender)        {            HttpApplication application = (HttpApplication)sender;            HttpResponse response = application.Context.Response;            string ex = ConfigurationManager.AppSettings["WebCacheExpires"];            int expires = string.IsNullOrEmpty(ex) ? 5 : Convert.ToInt32(ex);            //設置Expires為當前日期再加上60秒            response.Cache.SetExpires(DateTime.Now.AddSeconds(60 * expires));            //HttpCacheability枚舉類型會找個時間解釋下             //HttpCacheability. Public 指定響應能由客戶端和共享(代理)緩存進行緩存            response.Cache.SetCacheability(HttpCacheability.Public);            response.Cache.SetValidUntilExpires(true);        }        void Filter(object sender)        {            HttpApplication application = (HttpApplication)sender;            HttpContext context = application.Context;            int statusCode = context.Response.StatusCode;            if (statusCode==200)            {                string filePath = context.Request.FilePath;                string fileExtension = VirtualPathUtility.GetExtension(filePath);                if (fileExtension.Equals(".html") || fileExtension.Equals(".htm") || fileExtension.IndexOf(".") > 0)                {                    //判斷緩存是否存在,不存在加入緩存,調用生成靜態的類和方法                      //產品過期,移除靜態文件,302重定向                      //if (System.IO.File.Exists(context.Server.MapPath(filePath)))                    //{                    //    context.Response.WriteFile(context.Server.MapPath(filePath));                    //    context.Response.End();                    //}                }            }                    }    }}
View Code


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枞阳县| 张家口市| 崇文区| 怀宁县| 休宁县| 伊吾县| 正阳县| 惠来县| 双辽市| 江源县| 嘉义县| 台州市| 兰考县| 阳朔县| 波密县| 景泰县| 海城市| 北辰区| 晴隆县| 罗江县| 淄博市| 宿州市| 彩票| 衡东县| 勃利县| 望谟县| 乐亭县| 昌邑市| 肥乡县| 泰安市| 讷河市| 修武县| 灵台县| 延川县| 元江| 错那县| 建宁县| 扶风县| 大新县| 廊坊市| 上蔡县|