首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么使基于DropDownList的VaryByControl缓存的内容立刻无效

2013-12-13 
如何使基于DropDownList的VaryByControl缓存的内容立刻无效问题在第三步,请仔细看第三步第一步dropdownlis

如何使基于DropDownList的VaryByControl缓存的内容立刻无效
问题在第三步,请仔细看第三步

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

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



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


[解决办法]
你可以随意自定义控制机制。

例如写

<%@ 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);
    }

[解决办法]
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单元的缓存依赖,同时起作用。

热点排行