Asp.net2.0和sqlserver配合進(jìn)行緩存有2種方式:輪詢(xún)、查詢(xún)通知。
輪詢(xún)-----------主要是針對(duì)SQL (7.0, 2000, 2005);
查詢(xún)通知-----只針對(duì)Sql2005。
SqlCacheDependency,要實(shí)現(xiàn)該功能,需要采用如下步驟:
1在.net framework 2.0的安裝目錄下(默認(rèn)是WINNT\Microsoft.NET\Framework\v2.0.40607),啟動(dòng)一個(gè)叫aspnet_regsql.exe的命令行工具,比如:
aspnet_regsql -S localhost –U sa –P 123456 -d Pubs –ed
上面的意思是,指定了本地的數(shù)據(jù)庫(kù)服務(wù)器localhost,并指定了登陸的用戶(hù)名和密碼,并用參數(shù)-d指定了要采用哪一個(gè)數(shù)據(jù)庫(kù)(這里是指定了pubs)數(shù)據(jù)庫(kù),-ed參數(shù)表示是允許該數(shù)據(jù)庫(kù)使用sqlcachedependency功能。
接著,我們需要指定對(duì)哪一個(gè)表使用數(shù)據(jù)sqlcachedependency功能,如:
aspnet_regsql -S localhost –U sa -P 123456 -ed -d pubs -et -t authors
上面語(yǔ)句的意思是,指定對(duì)pubs數(shù)據(jù)庫(kù)中的authors表使用sqlcachedependency功能,-t參數(shù)后是表的名稱(chēng),-et是允許該表使用sqlcachedependency功能。其他的有關(guān)參數(shù)簡(jiǎn)單介紹如下:
參數(shù) |
參數(shù)意義 |
-? |
顯示該工具的幫助功能. |
-S |
后接的參數(shù)為數(shù)據(jù)庫(kù)服務(wù)器的名稱(chēng)或者IP地址 |
-U |
后接的參數(shù)為數(shù)據(jù)庫(kù)的登陸用戶(hù)名. |
-P |
后接的參數(shù)為數(shù)據(jù)庫(kù)的登陸密碼 |
-E |
當(dāng)使用windows集成驗(yàn)證時(shí),使用該功能 |
-t |
后接參數(shù)為對(duì)哪一個(gè)表采用sqlcachedependency功能。 |
-d |
后接參數(shù)為對(duì)哪一個(gè)數(shù)據(jù)庫(kù)采用sqlcachedependency功能 |
-ed |
允許對(duì)數(shù)據(jù)庫(kù)使用sqlcachedependency功能 |
-dd |
禁止對(duì)數(shù)據(jù)庫(kù)采用sqlcachedependency功能 |
-et |
允許對(duì)數(shù)據(jù)表采用sqlcachedependency功能 |
-dt |
禁止對(duì)數(shù)據(jù)表采用sqlcachedependency功能 |
-lt |
列出當(dāng)前數(shù)據(jù)庫(kù)中有哪些表已經(jīng)采用sqlcachedependency功能 |
Sqlcachedependency的的原理簡(jiǎn)單來(lái)說(shuō),是采用aspnet_regsql這個(gè)專(zhuān)門(mén)工具,預(yù)先設(shè)定要監(jiān)視的數(shù)據(jù)庫(kù)和表,并在數(shù)據(jù)庫(kù)中增加一個(gè)專(zhuān)門(mén)的表,打開(kāi)數(shù)據(jù)庫(kù),會(huì)發(fā)現(xiàn)多了一個(gè)數(shù)據(jù)表aspnet_sqlcachetablesforchangenotification.
|
這個(gè)表有三個(gè)字段,“tableName”記錄要追蹤的數(shù)據(jù)表的名稱(chēng),“notificationCreated”記錄開(kāi)始追蹤的時(shí)間,“changeId”是一個(gè)int類(lèi)型的字段,每當(dāng)追蹤的數(shù)據(jù)表的數(shù)據(jù)發(fā)生變化時(shí),這個(gè)字段的值就加1。 此外還會(huì)在指定的數(shù)據(jù)庫(kù)中增加幾個(gè)存儲(chǔ)過(guò)程,用來(lái)讓ASP.NET引擎查詢(xún)追蹤的數(shù)據(jù)表的情況,并給要使用 Sqlcachedependency 的表加上若干觸發(fā)器,分別對(duì)應(yīng)到Insert、Update、Delete操作。ASP.NET引擎通過(guò)執(zhí)行它加上的存儲(chǔ)過(guò)程“AspNet_SqlCachePollingStoredProcedure”,這個(gè)存儲(chǔ)過(guò)程直接返回“AspNet_SqlCacheTablesForChangeNotification”表的內(nèi)容,讓ASP.NET引擎知道哪個(gè)表的數(shù)據(jù)發(fā)生的變化。
查詢(xún)通知方式編程步驟:
與基于輪詢(xún)的失效不同,無(wú)需在應(yīng)用程序的配置中注冊(cè)任何 <sqlCacheDependency>。而且,無(wú)需使用 aspnet_regsql.exe 工具進(jìn)行任何特殊配置。
基于通知的依賴(lài)項(xiàng)是使用字符串 CommandNotification 在 OutputCache 指令上 配置的。此值告知 ASP.NET 應(yīng)為頁(yè)或數(shù)據(jù)源控件創(chuàng)建基于通知的 依賴(lài)項(xiàng)。
在頁(yè)上:
<%@ OutputCache Duration="999999" SqlDependency="CommandNotification" VaryByParam="none" %>
在數(shù)據(jù)源控件上:
<asp:SqlDataSource EnableCaching="true" SqlCacheDependency="CommandNotification" CacheDuration="Infinite" ... />
在首次執(zhí)行某 SQL 查詢(xún)之前,必須在應(yīng)用程序某處調(diào)用 System.Data.SqlClient.SqlDependency.Start() 方法。此方法應(yīng)放在 global.asax 文件的 Application_Start() 事件中。
使用 SqlCacheDependencyAdmin 類(lèi)
aspnet_regsql 實(shí)際上也是在后臺(tái)使用 SqlCacheDependencyAdmin 類(lèi)的方法來(lái)配置 Microsoft SQL Server??梢灾苯訌?/SPAN> ASP.NET 頁(yè)面中使用此類(lèi)的方法。
SqlCacheDependencyAdmin 類(lèi)具有五個(gè)重要的方法:
• |
DisableNotifications — 為特定數(shù)據(jù)庫(kù)禁用 SQL Cache Invalidation。 |
• |
DisableTableForNotifications — 為數(shù)據(jù)庫(kù)中的特定表禁用 SQL Cache Invalidation。 |
• |
EnableNotifications — 為特定數(shù)據(jù)庫(kù)啟用 SQL Cache Invalidation。 |
• |
EnableTableForNotifications — 為數(shù)據(jù)庫(kù)中的特定表啟用 SQL Cache Invalidation。 |
• |
GetTablesEnabledForNotifications — 返回啟用了 SQL Cache Invalidation 的所有表的列表。 |
需要注意的是,SQL Cache Invalidation 只能用于 Microsoft SQL Server 7 及更高版本,不能用于其他數(shù)據(jù)庫(kù),例如 Microsoft Access 或 Oracle。
列表 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 id="Head1" 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 ID="Button1" Text="Enable Table" OnClick="EnableTable"
Runat="Server" />
</form>
</body>
</html>
應(yīng)用
Web.config <connectionStrings> <add name="MyConn" connectionString="server=192.168.1.23;database=carsea;uid=sa;pwd=;" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <!-- 定義緩存策略--> <caching> <sqlCacheDependency enabled="true" pollTime="10000"> <databases> <!-- name:必需的 String 屬性。 要添加到配置集合中的 SqlCacheDependencyDatabase 對(duì)象的名稱(chēng)。 此名稱(chēng)用作 @ OutputCache 指令上 SqlDependency 屬性的一部分。 pollTime:設(shè)置 SqlCacheDependency 輪詢(xún)數(shù)據(jù)庫(kù)表以查看是否發(fā)生更改的頻率(以毫秒計(jì)算)。這兒是一個(gè)測(cè)試,所以設(shè)為10秒,請(qǐng)加大此值 --> <add connectionStringName="MyConn" name="board"/> </databases> </sqlCacheDependency> </caching> </system.web> |
using System.Web.Caching; string key = "model_type_" + type; //從緩存中取值 CBoardInfo model = (CBoardInfo)HttpRuntime.Cache[key]; if (model == null) { //取數(shù)據(jù) model = new CBoardTask().GetModelByType(type); //啟用更改通知 SqlCacheDependencyAdmin.EnableNotifications( ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString); //連接到 SQL Server 數(shù)據(jù)庫(kù)并為 SqlCacheDependency 更改通知準(zhǔn)備數(shù)據(jù)庫(kù)表 SqlCacheDependencyAdmin.EnableTableForNotifications( ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString, "board"); //制定緩存策略 SqlCacheDependency scd = new SqlCacheDependency("board", "board"); //插入緩存 HttpRuntime.Cache.Insert(key, model, scd); } return model; |
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇