65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET 2.0 中使用 SQL Server 缓存

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.42/5 (11投票s)

2007 年 3 月 22 日

2分钟阅读

viewsIcon

44530

使用缓存来减少 ASP.NET 2.0 中的回发时间

引言

这种 SQL 缓存失效机制仅适用于 SQL Server 7 或更高版本。

ASP.NET 2.0 中对缓存进行了改进。最有趣的功能是引入了数据库触发的缓存失效机制。

有三种方法可以进行基于 SQL 的失效。

1. 使用 OutputCache 指令进行声明式输出缓存。

2. 使用 SqlCacheDependency 对象进行程序化输出缓存。

3. 缓存 API

在旧的 Framework 1.x 中,有一个轮询机制来检查数据的有效性,但在 Framework 2.0 和 SQL Server 2005 中,这个过程完全反转,这意味着 SQL Server 2005 现在将通过使用 IIS 端口 80 发送 HTTP 请求来通知 ASP 页面数据更改。

您可以配置 SQL Server 2005,以便在数据库、数据库表或数据库行发生更改时通知您的 ASP.NET 应用程序。

要配置 SQL Server 进行缓存,我们可以使用 SqlCacheDependencyAdmin 类。

SqlCacheDependencyAdmin 类有五个重要的方法

  • DisableNotifications—禁用特定数据库的 SQL 缓存失效。
  • DisableTableForNotifications—禁用数据库中特定表的 SQL 缓存失效。
  • EnableNotifications—启用特定数据库的 SQL 缓存失效。
  • EnableTableForNotifications—启用数据库中特定表的 SQL 缓存失效。
  • GetTablesEnabledForNotifications—返回已启用 SQL 缓存失效的所有表的列表。

在 ASP.NET 2.0 中,我们还可以创建自定义缓存依赖项。


使用代码


//First you have add some lines into your Web.config file
<configuration>
      
  <connectionStrings>
    <add name="mySqlServer" 
      connectionString="Server=localhost;Database=Pubs" />
  </connectionStrings>
        
  <system.web>

    <caching>
      <sqlCacheDependency enabled="true">
      <databases>
      <add
            name="Pubs"
            connectionStringName="mySqlServer"
            pollTime="60000" />
      </databases>
      </sqlCacheDependency>
    </caching>
    </system.web>
</configuration>
//Within the <databases> subsection, you can list one or more databases that
//you want to poll for changes

使用页面输出缓存进行 SQL 缓存失效

 <%@ OutputCache SqlDependency="Pubs:Titles" 
    Duration="6000" VaryByParam="none" %>
<html>
<head runat="server">
    <title>Output Cache Titles</title>
</head>
<body>
    <form id="form1" runat="server">
    
    <%= DateTime.Now %>

    <asp:GridView 
      ID="grdTitles" 
      DataSourceID="SqlDataSource1" 
      Runat="Server" />    
    
    <asp:SqlDataSource
      ID="SqlDataSource1"
      SelectCommand="Select * FROM Titles"
      ConnectionString="<%$ ConnectionStrings:mySqlServer %>"
      Runat="Server" />
    
    </form>
</body>
</html>
//Notice that the SqlDependency attribute references the name of the 
//database defined within the Web configuration file. 

使用数据源控件进行 SQL 缓存失效

//You should consider using SQL Cache Invalidation with the DataSource 
//controls when you need to work with the same database data in multiple 
//pages. The SqlDataSource, AccessDataSource, and ObjectDataSource controls 
//all support a SqlCacheDependency property.
 <html>
<head id="Head1" runat="server">
    <title>SqlDataSource Caching</title>
</head>
<body>
    <form id="form1" runat="server">

        <%= DateTime.Now %>

        <asp:GridView 
            ID="grdTitles" 
            DataSourceId="SqlDataSource1"
            Runat="server" />
            
        <asp:SqlDataSource 
            ID="SqlDataSource1" 
            EnableCaching="true"
            SqlCacheDependency="Pubs:Titles"
            SelectCommand="select * from titles"
            ConnectionString="<%$ ConnectionStrings:mySqlServer %>"
            Runat="server" />
   
    </form>
</body>
</html>

缓存对数据库驱动的 Web 应用程序的性能有显著影响。幸运的是,ASP.NET 2.0 框架包含许多重要的增强功能,使您更容易在应用程序中利用缓存。

新的 DataSource 控件包含属性,可以轻松地将数据库数据缓存到内存中。通过利用 DataSource 控件,您可以检索数据库数据并缓存数据,而无需编写任何代码。

关注点

我喜欢与像您这样的人分享我的知识,因为我也是你们中的一员,如果我不知道任何事情,我会来找您...所以保持知识的交流...

历史

请继续关注我的简单方法系列文章....

© . All rights reserved.