假如我在asp.net要给一个数据库表写一个缓存依赖到的该怎么写,麻烦哪位大侠指点下。

解决方案 »

  1.   

    SqlCacheDependency
    ASP.NET 缓存
      

  2.   

    SqlCacheDependency
    MSDN:
    http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx
      

  3.   

    大多数时候,或许根本不需要SqlCacheDependency。先看看下面这个例子,两个aspx:
    default.aspx
    <%@ Page Language="C#" %><script runat="server">
        void Button1_Click1(object sender, EventArgs e)
        {
            Cache["bb时间戳"] = DateTime.Now.ToString();
        }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" />
        </form>
    </body>
    </html>
    default2.aspx<%@ Page Language="C#" %>
    <%@ OutputCache Duration="6000" VaryByParam="*" VaryByCustom="CacheBy:aa标记,bb时间戳" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <% =DateTime.Now.ToString() %>
        </div>
        </form>
    </body>
    </html>
    而global.asax中我们增加了这样的一个方法:public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom.StartsWith("CacheBy:"))
        {
            var ss = string.Empty;
            foreach (string s in custom.Substring(8).Split(','))
            {
                string xx = context.Cache[s] as string;
                ss += (xx == null ? string.Empty : xx);
            }
            return ss;
        }
    }什么意思呢?我们设置default2.aspx根据两个Cache单元的值的变化而刷新,而default.asx中我们修改其中一个单元的值。在浏览器上打开这两个页面,可以看到刷新default2.aspx时时间不会变化(因为根本没有重新生成页面而是直接拿出html缓存流输出),而如果点击default.aspx上的按钮之后,default2.aspx就会被及时刷新一次。这说明了我们可以自定义系统全局的“暗号”,来告诉哪些类型的数据应该刷新缓存。当你写数据到数据库里时,同时更新一下暗号,那么所有关联到它的aspx、ascx、其它cache单元等等,就都自动刷新了。很简单吧!有时候不需要SqlCacheDependency,可以自定义很强的缓存依赖系统。当然,最好不像这个demo这么土,最好包装成一个框架子系统,看上去也漂亮。
      

  4.   

    上面我举了一个OutputCache的小例子(对于Ascx也是一样的),我在举一个控制数据缓存依赖项的例子。还是扩展一下default.aspx<%@ Page Language="C#" %><script runat="server">
        void Button1_Click1(object sender, EventArgs e)
        {
            Cache["bb时间戳"] = DateTime.Now.ToString();
        }    string Get1234()
        {
            string key = "sp1234's SQL query";
            string x = Cache[key] as string;
            if (x == null)
            {
                x = DateTime.Now.ToString();
                Cache["bb时间戳"] = DateTime.Now.ToString();
                Cache.Insert(key, x, new CacheDependency(new string[] { }, new string[] { "bb时间戳" }));
            }
            return x;
        }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" />
        <br />
        <% = Get1234() %>
        <br />
        <asp:Button ID="Button2" runat="server" Text="Button" />
        </form>
    </body>
    </html>
    可以直接测试这个页面。这里,用一个键值为“bb时间戳”的Cache单元来控制键值为“sp1234's SQL query”的混存单元进行刷新(实际上是清除)。这个例子中我向后者放入一个字符串,在实际开发则往往是一个复杂的对象,甚至是为一次分页查询所获得的包括几十条对象的一个集合对象。