如何使基于DropDownList的VaryByControl缓存的内容立刻无效

Ptrtoptr 2013-12-11 11:01:11
问题在第三步,请仔细看第三步

第一步
dropdownlist里有三项,分别代表三种类别的书:C#,C++,Java
通过dropdownlist选择不同的类别后,触发的drpBookSort_selectedIndexChanged事件会把
属于被选中类别的所有书都显出来了,第一步很正常。

第二步
为了提高速度我用了如下缓存
<%@ OutputCache Duration="1000" VaryByParam="none" VaryByControl="drpBookSort"%>
现在虽然不会触发drpBookSort_selectedIndexChanged事件,
但是通过dropdownlist选择不同的类别后,仍然能显示不同类别的所有书,第二步也正常



第三步
问题是当我在C#类别下又新加一本书比如《C#高级编程》,可是由于缓存的原因,我发现新加的书显不出来,
而且drpBookSort_selectedIndexChanged事件,也因为缓存的原因不被触发了,
请问有没有简单的代码,使那个VaryByControl的缓存暂时无效,把新加的数据也显示出来,然后再缓存

...全文
247 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ptrtoptr 2013-12-12
  • 打赏
  • 举报
回复
代码我花了快1小时看懂了,非常感谢,只是有个小疑问. 什么Asp.net不直接提供一种爽快的方法来使缓存无效, 而使要通过加一个varyByCustom 再加一个Cache来搞定这个问题.
  • 打赏
  • 举报
回复
asp.net 的页面(或者片段)Cache很完整可靠,你可以组合不同的 Varyby。 因此上述的代码还是太麻烦了,而且也没有自动处理DropDownList1控件的生成不一样的ClientID的问题)。 所以还是用asp.net自己的办法更可取,你完全可以写
<%@ Page Language="C#" %>

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByControl="DropDownList1" 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")
        {
            return (string)context.Cache["t1"] ?? string.Empty;
        }
        return base.GetVaryByCustomString(context, custom);
    }
于是,你的基于 DropDownList 控件的缓存依赖,跟新增的基于自定义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: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);
    }
铁歌 2013-12-11
  • 打赏
  • 举报
回复
JS AJAX提交即时显示出来.

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧