ASP.NET 2.0 中改進的快取功能
使用 SqlCacheDependencyAdmin 類
aspnet_regsql 工具在後臺使用 SqlCacheDependencyAdmin 類的方法來配置 Microsoft SQL Server。如果您願意,可以直接從 ASP.NET 頁面中使用此類的方法。
SqlCacheDependencyAdmin 類具有五個重要的方法:
• | DisableNotifications — 為特定資料庫禁用 SQL Cache Invalidation。 |
• | DisableTableForNotifications — 為資料庫中的特定表禁用 SQL Cache Invalidation。 |
• | EnableNotifications |
• | EnableTableForNotifications — 為資料庫中的特定表啟用 SQL Cache Invalidation。 |
• | GetTablesEnabledForNotifications — 返回啟用了 SQL Cache Invalidation 的所有表的列表。 |
例如,使用列表 2 中的 ASP.NET 頁面,您可以為 Pubs 資料庫中的任何表配置 SQL Cache Invalidation(參見圖 2)。
圖 2:從 ASP.NET 頁面中啟用 SQL Cache Invalidation
列表 2:EnableSCI.aspx (C#)
<%@ Page Language="c#" %> <%@ Import Namespace="System.Web.Caching" %> <script runat="server"> const string connectionString = "Server=localhost;Database=Pubs"; void Page_Load() { if (!IsPostBack) { SqlCacheDependencyAdmin.EnableNotifications( connectionString); SqlDataSource1.SelectParameters.Add("connectionString", connectionString); } } void EnableTable(Object s, EventArgs e) { try { SqlCacheDependencyAdmin.EnableTableForNotifications( connectionString, txtTableName.Text); } catch (Exception ex) { lblErrorMessage.Text = ex.Message; } txtTableName.Text = ""; } </script> <html> <head runat="server"> <title>Enable SQL Cache Invalidation</title> </head> <body> <form id="form1" runat="server"> <h1>SQL Cache Invalidation</h1>
以下表格已啟用 SQL Cache Invalidation:
<p> <asp:GridView id="grdTables" DataSourceID="SqlDataSource1" CellPadding="10" ShowHeader="false" Runat="Server" /> </p> <asp:ObjectDataSource ID="SqlDataSource1" TypeName="System.Web.Caching.SqlCacheDependencyAdmin" SelectMethod="GetTablesEnabledForNotifications" Runat="Server" /> <p> <asp:Label ID="lblErrorMessage" EnableViewState="false" ForeColor="red" Runat="Server" /> </p> <asp:TextBox ID="txtTableName" Runat="Server" /> <asp:Button Text="Enable Table" OnClick="EnableTable" Runat="Server" /> </form> </body> </html>
列表 2:EnableSCI.aspx (Visual Basic .NET)
<%@ Page Language="vb" %> <%@ Import Namespace="System.Web.Caching" %> <script runat="server"> Const connectionString As String = "Server=localhost;Database=Pubs" Sub Page_Load() If Not IsPostBack Then SqlCacheDependencyAdmin.EnableNotifications( _ connectionString) SqlDataSource1.SelectParameters.Add("connectionString", _ connectionString) End If End Sub Sub EnableTable(ByVal s As Object, ByVal e As EventArgs) Try SqlCacheDependencyAdmin.EnableTableForNotifications( _ connectionString, txtTableName.Text) Catch ex As Exception lblErrorMessage.Text = ex.Message End Try txtTableName.Text = "" End Sub </script> <html> <head id="Head1" runat="server"> <title>ConfigureSCI</title> </head> <body> <form id="form1" runat="server"> <h1>SQL Cache Invalidation</h1>
以下表格已啟用 SQL Cache Invalidation:
<p> <asp:GridView id="grdTables" DataSourceID="SqlDataSource1" CellPadding="10" ShowHeader="false" Runat="Server" /> </p> <asp:ObjectDataSource ID="SqlDataSource1" TypeName="System.Web.Caching.SqlCacheDependencyAdmin" SelectMethod="GetTablesEnabledForNotifications" Runat="Server" /> <p> <asp:Label ID="lblErrorMessage" EnableViewState="false" ForeColor="red" Runat="Server" /> </p> <asp:TextBox ID="txtTableName" Runat="Server" /> <asp:Button ID="Button1" Text="Enable Table" OnClick="EnableTable" Runat="Server" /> </form> </body> </html>
在列表 2 中,connectionString 常量用於選擇啟用了 SQL Cache Invalidation 的資料庫(如果要為 Pubs 資料庫以外的資料庫啟用 SQL Cache Invalidation,可以更改此常量的值)。在 Page_Load 方法中,呼叫 SqlCacheDependencyAdmin 類上的 EnableNotifications 方法,為由 connectionString 常量指定的資料庫啟用 SQL Cache Invalidation。
列表 2 中的 GridView 顯示了當前啟用了 SQL Cache Invalidation 的所有資料庫表。GridView 被繫結到 ObjectDataSource 控制元件上,該控制元件為其 SelectMethod 呼叫 GetTablesneabledForNotifications 方法。
最後,您可以使用列表 2 中的頁面為其他表啟用 SQL Cache Invalidation。在文字框中輸入表的名稱並單擊“Enable Table”按鈕時,將呼叫 EnableTableForNotifications 方法。