|
本文實例講述了ASP.NET中SqlCacheDependency緩存技術(shù),對于大型web程序設(shè)計來說具有很高的實用價值。具體如下:
對于訪問量大,但更新較少的網(wǎng)站中使用緩存技術(shù),可以大大提高運行效率;加上.NET 2.0提供的緩存依賴機制,我們可以很方便的對緩存進(jìn)行管理更新;以下是本人學(xué)習(xí)的一點心得體會,希望能夠起到拋磚引玉的作用。
建立緩存依賴,實現(xiàn)代碼如下:
/**//// <summary> /// 建立緩存依賴項 /// </summary> /// <returns></returns> private AggregateCacheDependency TableDependency() { AggregateCacheDependency dependency = new AggregateCacheDependency(); dependency.Add(new SqlCacheDependency("MSPetShop4", "表名稱")); return dependency; }
一個非常簡單的方法,首先我們先看看兩個.NET 2.0新增的兩個類:
AggregateCacheDependency在System.Web.Caching命名空間中,AggregateCacheDependency主要作用是用于組合 ASP.NET 應(yīng)用程序的 Cache 對象中存儲的項和 CacheDependency 對象的數(shù)組之間的多個依賴項。
SqlCacheDependency也存在于System.Web.Caching命名空間中,這個類用于建立ASP.NET應(yīng)用程序的Cache對象中存儲的項和特定SQL Server數(shù)據(jù)庫表之間的聯(lián)系。
SqlCacheDependency是如何建立Cache對象中存儲的項和特定SQL Server數(shù)據(jù)庫表之間的聯(lián)系的呢?看一下Web.Config配置文件就一目了然了。
<?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NETConfiguration/v2.0"> <connectionStrings> <add name="LocalConnString" connectionString="Server=(Local);uid=sa;pwd=123456;DataBase=MSPetShop4"/> </connectionStrings> <system.web> <caching> <sqlCacheDependency enabled="true" pollTime="10000"> <databases> <add name="MSPetShop4" connectionStringName="LocalConnString" pollTime="10000"/> </databases> </sqlCacheDependency> </caching> <compilation debug="true"/> </system.web> </configuration>
配置節(jié)<databases><add name="MSPetShop4" connectionStringName="LocalConnString" pollTime="10000"/></databases>中配置了數(shù)據(jù)庫信息,SqlCacheDependency類會自動完成對此配置節(jié)信息的讀取以建立和數(shù)據(jù)庫之間的聯(lián)系。(注意)name="MSPetShop4"必須和new SqlCacheDependency("MSPetShop4", "表名稱")中的數(shù)據(jù)庫名稱相一致。更多的配置信息可以查看(MSDN幫助文檔)。
使數(shù)據(jù)庫支持SqlCacheDependency特性:
要使得7.0或者2000版本的SQL Server支持SqlCacheDependency特性,需要對數(shù)據(jù)庫服務(wù)器執(zhí)行相關(guān)的配置步驟。有兩種方法配置SQL Server:
使用ASPNET_regsql命令行工具,或者使用SqlCacheDependencyAdmin類。
ASPNET_regsql工具位于Windows/Microsoft.NET/Framework/[版本]文件夾中,如果要配置SqlCacheDependency,則需要以命令行的方式執(zhí)行。
以下是該工具的命令參數(shù)說明:
-? 顯示該工具的幫助功能;
-S 后接的參數(shù)為數(shù)據(jù)庫服務(wù)器的名稱或者IP地址;
-U 后接的參數(shù)為數(shù)據(jù)庫的登陸用戶名;
-P 后接的參數(shù)為數(shù)據(jù)庫的登陸密碼;
-E 當(dāng)使用windows集成驗證時,使用該功能;
-d 后接參數(shù)為對哪一個數(shù)據(jù)庫采用SqlCacheDependency功能;
-t 后接參數(shù)為對哪一個表采用SqlCacheDependency功能;
-ed 允許對數(shù)據(jù)庫使用SqlCacheDependency功能;
-dd 禁止對數(shù)據(jù)庫采用SqlCacheDependency功能;
-et 允許對數(shù)據(jù)表采用SqlCacheDependency功能;
-dt 禁止對數(shù)據(jù)表采用SqlCacheDependency功能;
-lt 列出當(dāng)前數(shù)據(jù)庫中有哪些表已經(jīng)采用sqlcachedependency功能。
比如在petshop4.0的數(shù)據(jù)庫中使用SqlCacheDependency特性:ASPNET_regsql -S localhost -E -d MSPetShop4 -ed
以上面的命令為例,說明將對名為MSPetShop4的數(shù)據(jù)庫采用SqlCacheDependency功能,且SQL Server采用了windows集成驗證方式。我們還可以對相關(guān)的數(shù)據(jù)表執(zhí)行ASPNET_regsql命令,如:
ASPNET_regsql -S localhost -E -d MSPetShop4 -t Item -et ASPNET_regsql -S localhost -E -d MSPetShop4 -t Product -et ASPNET_regsql -S localhost -E -d MSPetShop4 -t Category -et
最后為使用緩存:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string key = "TableCache"; //緩存名稱 DataSet data = (DataSet)HttpRuntime.Cache[key]; //獲取緩存 // 判斷緩存數(shù)據(jù)為空 if (data == null) { // 獲取數(shù)據(jù) data = GetDataSource(); // 創(chuàng)建緩存依賴 AggregateCacheDependency cd = TableDependency(); // 創(chuàng)建緩存 HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.High, null); } GridView1.DataSource = data; //綁定數(shù)據(jù) GridView1.DataBind(); } }
獲取數(shù)據(jù)源的方法,結(jié)合實際使用做修改。
private DataSet GetDataSource() { string ConnectionStringLocal = ConfigurationManager.ConnectionStrings["LocalConnString"].ConnectionString; SqlConnection connPubs = new SqlConnection(ConnectionStringLocal); SqlDataAdapter dad = new SqlDataAdapter("SELECT TOP 50 * FROM Product", connPubs); DataSet ds = new DataSet(); dad.Fill(ds); return ds; }
希望本文所述緩存技術(shù)對大家ASP.NET程序設(shè)計有所幫助。
AspNet技術(shù):asp.net中SqlCacheDependency緩存技術(shù)概述,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。