这里需要注意的重要一点是,如果您的VaryByCustom发生更改,则asp.net会存储单独的缓存版本的页面。
因此,假设你的VaryByCustom-“custom1”的Duration =“60”,那么这将被缓存60秒。
如果您的VaryByCustom更改为“custom2”且duration =“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);
}
}
.aspx的
<%@ 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" %>
页面加载
protected void Page_Load(object sender, EventArgs e)
{
lblTime.Text = "The time now is: <br/>";
lblTime.Text += DateTime.Now.ToString();
}
我还没有包括对ASP标记:在ASPX标签,但我希望你能总体思路。
这是一个很酷的问题,但我仍然没有接近答案。我一直在试图建立一个测试场景。你可以发布你的'@ OutputCache'指令和'GetVaryByCustomString'函数覆盖吗? PS:国际海事组织关于缓存的MSDN文献似乎是一个令人困惑的垃圾负载。 – pseudocoder 2011-06-02 15:15:59