ASP.NET 4.0中的不同服务器和客户端缓存策略

rou*_*tic 5 asp.net outputcache duration

我对ASP.NET输出缓存缺乏一点基本的了解.

就我而言,我的资源与我的VaryByCustom密钥密切相关.在服务器端,我喜欢这些密钥无限期地缓存,直到密钥发生变化.这些缓存条目没有理由在计时器上清除.

但是,客户应该每小时检查一次,以获得服务器认为最新鲜的内容.

如果我将持续时间设置为1小时,我知道在客户端上正确设置了过期标头.但它是否也驱逐了服务器端缓存?有没有办法确保响应保持缓存在服务器上,直到我的VaryByCustom更改,但仍然允许更频繁的客户端到期?

sha*_*shi 0

这里需要注意的重要一点是,如果您的 VaryByCustom 发生更改,asp.net 会存储页面的单独缓存版本。

因此,假设您的 VaryByCustom-"custom1" 的 Duration="60" 那么这将被缓存 60 秒。

在此期间,如果您的 VaryByCustom 更改为“custom2”且持续时间=“60”,则 asp.net 将在缓存中存储一​​个单独的副本,该副本与“custom1”的缓存不同,并且此单独版本将缓存 60 秒。

测试这一点的一个简单方法是根据浏览器更改 VaryByCustom。

在 Global.asax 中

public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom == "browser")
            {
                string browserName;
                browserName = Context.Request.Browser.Browser;
                browserName += Context.Request.Browser.MajorVersion.ToString();

                return browserName;
            }
            else
            {
                return base.GetVaryByCustomString(context, custom);
            }

        }
Run Code Online (Sandbox Code Playgroud)

阿斯克斯

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebFormsPlayGround._Default" %>
    <%@ OutputCache Duration="40" VaryByParam="None" VaryByCustom="browser" %>
Run Code Online (Sandbox Code Playgroud)

页面加载

protected void Page_Load(object sender, EventArgs e)
        {
            lblTime.Text = "The time now is: <br/>";
            lblTime.Text += DateTime.Now.ToString();
        }
Run Code Online (Sandbox Code Playgroud)

我没有在 aspx 中包含 asp:Label 的标记,但我希望您能了解总体思路。