1:减少静态页面请求
要让静态页面支持这个需求,我们需要用到http头中的Cache-Control: max-age。值得注意的是Cache-Control是在HTTP/1.1协议下的标识,它是HTTP/1.0协议中的Expires的升级。为了让静态页支持Cache-Control,一种方案是在IIS中进行设置,如下,我在需要静态缓存的页面或者文件夹上右键->属性:
可以看到其得到的http头中已经有了Cache-Control: max-age=60这一项。
现在,我需要在1分钟内反复请求该静态页,请求的行为我们分别通过下面几种方式来实现,
F5代表浏览器的一次刷新,它对Last-Modified有效,但是对Cache-Control无效
点击“转到”或者光标移入地址栏然后回车对Cache-Control有效
CTRL+F5强制刷新,返回所有正文
我们通过HttpWatch得到的结果如下:
</staticContent> 
</system.webServer> 
复制代码 代码如下:
 
<location path="test2.htm"> 
<system.webServer> 
<staticContent> 
<clientCache cacheControlMode="DisableCache"/> 
</staticContent> 
</system.webServer> 
</location> 
复制代码 代码如下:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
//处理点击“转到”或者光标移入地址栏然后回车,也就是本文所阐述的 
this.Response.AddHeader("Cache-Control", "max-age=60"); 
//真是用来处理F5刷新的,也就是对Last-Modified有效 
this.Response.AddHeader("Last-Modified", DateTime.Now.ToString("U", DateTimeFormatInfo.InvariantInfo)); 
DateTime IfModifiedSince; 
if (DateTime.TryParse(this.Request.Headers.Get("If-Modified-Since"), out IfModifiedSince)) 
{ 
if ((DateTime.Now - IfModifiedSince.AddHours(8)).Seconds < 60) 
{ 
Response.Status = "304 Not Modified"; 
Response.StatusCode = 304; 
return; 
} 
} 
} 
结果如下:
</outputCacheProfiles> 
</outputCacheSettings> 
</caching> 
</system.web> 
复制代码 代码如下:
 
<%@ OutputCache CacheProfile="cache1" %> 
新闻热点
疑难解答
图片精选