Http请求头缓存设置方法

1、直接在.aspx页面中设置
最直接的,在.aspx页面中添加一行如下代码:

<%@ OutputCache Duration="3600" VaryByParam="None" %>

表示将这个页面缓存1小时。运行页面查看请求头信息:
第一次运行,效果如图:

再次请求页面
点击“转到”或者光标移入地址栏然后回车,或者F5刷新页面,效果如图:

注意:缓存对ctrl+F5强刷不起作用。

可以看到,设置后请求响应头中多了max-age、Expires、Last-Modified这三个属性,并且Http状态码也由200变成了304,说明我们成功设置了该页面的缓存。
对比两次请求头里的Cache-Control,发现第一次是no-cache,第二次是max-age=0,并且第三、第四。。次都是max-age=0,关于Cache-Control值的说明如下:
如果no-cache出现在请求中,则代表浏览器要求服务器,此次请求必须重新返回最新文件(请求完成后,你会看到http状态码是200);如果max-age=0出现在响应中,则代表服务器要求浏览器你在使用本地缓存的时候,必须先和服务器进行一遍通信,将etag、 If-Not-Modified等字段传递给服务器以便验证当前浏览器端使用的文件是否是最新的(如果浏览器用的是最新的文件,http状态码返回304,服务器告诉浏览器使用本地缓存即可;否则返回200,浏览器得自己吧文件重新下载一遍)。

请求页面方式说明:

首次请求 返回状态码200,显然得到全部正文。
F5 刷新,对Last-Modified有效,它是让服务器判断是否需要读取缓存,所以,依然存在请求和返回数据。状态码是304。
点击“转到”或者光标移入地址栏然后回车 对Cache-Control有效,是浏览器自己决定是否读取缓存,如果在缓存期限内,浏览器不会向WEB服务器发送请求,我们可以看到send和receive的数据全部是0。无交互,故无状态码。
ctrl+f5 相当于是强制刷新,所以状态码200 OK,返回全部正文数据。

2、在.cs代码中设置

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HttpTest
{
    public partial class HttpTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int intHttpCacheMaxAge = 0; //浏览器缓存时间,单位:秒
                string strHttpCacheMaxAge = ConfigurationManager.AppSettings["httpCacheMaxAge"];
                if (!string.IsNullOrEmpty(strHttpCacheMaxAge))
                {
                    intHttpCacheMaxAge = Convert.ToInt32(strHttpCacheMaxAge);
                }

                //设置缓存过期变量,默认为缓存时间多1秒,即第一次请求总让它返回200
                int seconds = intHttpCacheMaxAge + 1;
                DateTime? IfModifiedSince = null;
                if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"]))
                {
                    IfModifiedSince = Convert.ToDateTime(Request.Headers["If-Modified-Since"]);
                    seconds = (DateTime.Now - Convert.ToDateTime(IfModifiedSince)).Seconds;
                }
                if (seconds > intHttpCacheMaxAge)
                {
                    Common.Common.currDateTime = DateTime.Now;
                    Response.Cache.SetCacheability(HttpCacheability.Public);
                    Response.Cache.SetExpires(Convert.ToDateTime(Common.Common.currDateTime).AddSeconds(intHttpCacheMaxAge));
                    Response.Cache.SetLastModified(Convert.ToDateTime(Common.Common.currDateTime));
                }
                else
                {
                    Response.Status = "304 Not Modified..";
                    Response.StatusCode = 304;
                    Response.Cache.SetCacheability(HttpCacheability.Public);
                    Response.Cache.SetExpires(Convert.ToDateTime(Common.Common.currDateTime).AddSeconds(intHttpCacheMaxAge));
                    Response.Cache.SetLastModified(Convert.ToDateTime(Common.Common.currDateTime));
                    ////上面设置了响应请求状态为304后,我们不希望再继续执行其他的业务逻辑了,所以,
                    ////要阻止后续代码的执行,有以下两种方法:
                    ////方法一:使用CompleteRequest()方法直接执行EndRequest事件,如:
                    //HttpApplication app = (HttpApplication)sender;
                    //app.CompleteRequest();
                    ////方法二:使用return跳出代码执行,不执行return后面的任何代码,如:
                    return;
                }
            }
            catch (Exception ex)
            {
                Response.Write("异常!" + ex);
            }
        }
    }
}

 配置文件:

 <!--设置浏览器缓存时间,单位:秒-->
<add key="httpCacheMaxAge" value="10"/>

 这种方法可以针对单个页面设置,比较灵活。


3、利用HttpModule和HttpWorkerRequest设置
相比前两种,这种方法效率高些。
为了利用HttpModule,所以我们先要创建一个实现了IHttpModule接口的类,如:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace HttpTest.Module
{
    public class HttpCacheModule:IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.EndRequest += context_EndRequest;
        }
        void context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            IServiceProvider provider = (IServiceProvider)app.Context;
            HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
           
            int intHttpCacheMaxAge = 0; //浏览器缓存时间,单位:秒
            string strHttpCacheMaxAge = ConfigurationManager.AppSettings["httpCacheMaxAge"];
            if (!string.IsNullOrEmpty(strHttpCacheMaxAge))
            {
                intHttpCacheMaxAge = Convert.ToInt32(strHttpCacheMaxAge);
            }
            //设置缓存过期变量,默认为缓存时间多1秒,即第一次请求总让它返回200
            int seconds = intHttpCacheMaxAge+1;
            DateTime? IfModifiedSince = null;
            if (app.Context.Request.Headers["If-Modified-Since"]!=null)
            {
                IfModifiedSince = Convert.ToDateTime(app.Context.Request.Headers["If-Modified-Since"]);
                seconds = (DateTime.Now - Convert.ToDateTime(IfModifiedSince)).Seconds;
            }
            if (seconds > intHttpCacheMaxAge)
            {
                Common.Common.currDateTime = DateTime.Now;
            }
            else
            {
                app.Context.Response.Status = "304 Not Modified..";
                app.Context.Response.StatusCode = 304;
            }
            worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderCacheControl, "public");
            worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderExpires, Convert.ToDateTime(Common.Common.currDateTime).AddSeconds(intHttpCacheMaxAge).ToUniversalTime().ToString("r"));
            worker.SendKnownResponseHeader(HttpWorkerRequest.HeaderLastModified, Convert.ToDateTime(Common.Common.currDateTime).ToUniversalTime().ToString("r"));
        }

        public void Dispose()
        {
        }
      
    }
}

 配置文件:

IIS6中按以下配置:
<system.web>
	<httpModules>
	  <add name="HttpCacheModule" type="HttpTest.Module.HttpCacheModule,HttpTest"/>
	</httpModules>
</system.web>
IIS7及后续版中本:
<system.webServer>
	<modules runAllManagedModulesForAllRequests="true">
	  <add name="HttpCacheModule" type="HttpTest.Module.HttpCacheModule,HttpTest"/>
	</modules>
</system.webServer>

 另外,为了保存当前时间做缓存过期判断,所以定义一个静态变量,如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HttpTest.Common
{
    public static class Common
    {
        public static DateTime? currDateTime = null;
    }
}

 并且在Global文件的Application_Start方法中初始化它,如:

void Application_Start(object sender, EventArgs e)
{
    Common.Common.currDateTime = DateTime.Now;
}

 这种方式适合对所有页面都有效,适合批量设置。


参考资料:
https://www.cnblogs.com/fish-li/archive/2012/01/11/2320027.html
https://segmentfault.com/q/1010000002578217
https://www.cnblogs.com/ajunForNet/archive/2012/05/19/2509232.html
https://www.cnblogs.com/futan/archive/2013/04/21/cachehuancun.html
https://www.cnblogs.com/yuanyuan/p/4921717.html(HttpModule详解)
http://www.cnblogs.com/yuanyuan/archive/2010/11/15/1877709.html
http://www.cnblogs.com/luminji/archive/2011/09/14/2174751.html
http://www.cnblogs.com/luminji/archive/2011/09/13/2172737.html

原文地址:https://www.cnblogs.com/zhaow/p/7832829.html