你可以随意自定义控制机制。例如写<%@ Page Language="C#" %><%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="mytest1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
this.Label1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>测试VaryByCustom</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now.ToString() %>" EnableViewState="false"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
以及在global.asax中写 public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "mytest1")
{
var val = context.Cache["t1"];
if (val == null)
return string.Empty;
else
return (string)val;
}
return base.GetVaryByCustomString(context, custom);
}这时候执行不断刷新页面,你可以看到测试页面没有改变内容。如果你另外写一个页面,它执行HttpRuntime.Cache["t1"] = DateTime.Now.Ticks.ToString(); //知识为了确保产生一个新的、不一样的值
再来刷新之前的测试页,你会发现内容改变了一次!因此,你什么时候想让可以通过改变一个 Cache[标志] 单元的值,自己通知页面刷新。进一步,可以稍微复杂一点,例如<%@ Page Language="C#" %><%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="mytest1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
this.Label1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>测试VaryByCustom</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now.ToString() %>" EnableViewState="false"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>title1</asp:ListItem>
<asp:ListItem>title2</asp:ListItem>
<asp:ListItem>title3</asp:ListItem>
<asp:ListItem>title4</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>和代码 public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "mytest1")
{
var flag = new StringBuilder();
flag.AppendLine((string)context.Cache["t1"]);
flag.AppendLine(context.Request.Form["DropDownList1"]);
return flag.ToString();
}
return base.GetVaryByCustomString(context, custom);
}
<script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
this.Label1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>测试VaryByCustom</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now.ToString() %>" EnableViewState="false"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
以及在global.asax中写 public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "mytest1")
{
var val = context.Cache["t1"];
if (val == null)
return string.Empty;
else
return (string)val;
}
return base.GetVaryByCustomString(context, custom);
}这时候执行不断刷新页面,你可以看到测试页面没有改变内容。如果你另外写一个页面,它执行HttpRuntime.Cache["t1"] = DateTime.Now.Ticks.ToString(); //知识为了确保产生一个新的、不一样的值
再来刷新之前的测试页,你会发现内容改变了一次!因此,你什么时候想让可以通过改变一个 Cache[标志] 单元的值,自己通知页面刷新。进一步,可以稍微复杂一点,例如<%@ Page Language="C#" %><%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="mytest1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
this.Label1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>测试VaryByCustom</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now.ToString() %>" EnableViewState="false"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>title1</asp:ListItem>
<asp:ListItem>title2</asp:ListItem>
<asp:ListItem>title3</asp:ListItem>
<asp:ListItem>title4</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>和代码 public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "mytest1")
{
var flag = new StringBuilder();
flag.AppendLine((string)context.Cache["t1"]);
flag.AppendLine(context.Request.Form["DropDownList1"]);
return flag.ToString();
}
return base.GetVaryByCustomString(context, custom);
}
<script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
this.Label1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>测试VaryByCustom</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now.ToString() %>" EnableViewState="false"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>title1</asp:ListItem>
<asp:ListItem>title2</asp:ListItem>
<asp:ListItem>title3</asp:ListItem>
<asp:ListItem>title4</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>和代码 public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "mytest1")
{
return (string)context.Cache["t1"] ?? string.Empty;
}
return base.GetVaryByCustomString(context, custom);
}于是,你的基于 DropDownList 控件的缓存依赖,跟新增的基于自定义Cache单元的缓存依赖,同时起作用。
什么Asp.net不直接提供一种爽快的方法来使缓存无效,
而使要通过加一个varyByCustom 再加一个Cache来搞定这个问题.