asp.net中SqlCacheDependency緩存技術(shù)概述
本文實例講述了asp.net中SqlCacheDependency緩存技術(shù),對于大型web程序設計來說具有很高的實用價值。具體如下:
對于訪問量大,但更新較少的網(wǎng)站中使用緩存技術(shù),可以大大提高運行效率;加上.NET 2.0提供的緩存依賴機制,我們可以很方便的對緩存進行管理更新;以下是本人學習的一點心得體會,希望能夠起到拋磚引玉的作用。
建立緩存依賴,實現(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 應用程序的 Cache 對象中存儲的項和 CacheDependency 對象的數(shù)組之間的多個依賴項。
SqlCacheDependency也存在于System.Web.Caching命名空間中,這個類用于建立ASP.NET應用程序的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ù)庫服務器執(zhí)行相關(guān)的配置步驟。有兩種方法配置SQL Server:
使用aspnet_regsql命令行工具,或者使用SqlCacheDependencyAdmin類。
aspnet_regsql工具位于Windows\Microsoft.NET\Framework\[版本]文件夾中,如果要配置SqlCacheDependency,則需要以命令行的方式執(zhí)行。
以下是該工具的命令參數(shù)說明:
-? 顯示該工具的幫助功能;
-S 后接的參數(shù)為數(shù)據(jù)庫服務器的名稱或者IP地址;
-U 后接的參數(shù)為數(shù)據(jù)庫的登陸用戶名;
-P 后接的參數(shù)為數(shù)據(jù)庫的登陸密碼;
-E 當使用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 列出當前數(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程序設計有所幫助。
- Asp.Net Cache緩存使用代碼
- ASP.net Substitution 頁面緩存而部分不緩存的實現(xiàn)方法
- asp.net 客戶端瀏覽器緩存的Http頭介紹
- asp.net(C#)遍歷memcached緩存對象
- ASP.NET性能優(yōu)化之讓瀏覽器緩存動態(tài)網(wǎng)頁的方法
- ASP.NET網(wǎng)站管理系統(tǒng)退出 清除瀏覽器緩存,Session的代碼
- ASP.NET緩存管理的幾種方法
- C#緩存之SqlCacheDependency用法實例總結(jié)
- asp.net頁面SqlCacheDependency緩存實例
- 在ASP.NET 2.0中操作數(shù)據(jù)之五十九:使用SQL緩存依賴項SqlCacheDependency
相關(guān)文章
asp.net 獲取目錄下的文件數(shù)和文件夾數(shù)
遍歷一個文件夾中的文件,需要用到DirectoryInfo類中的一個重要的方法GetFileSystemInfos(),此方法返回指定的是與搜索條件相匹配的文件和子目錄的強類型 FileSystemInfo對象的數(shù)組。2010-07-07
ASP.NET中實現(xiàn)jQuery Validation-Engine的Ajax驗證
在jQuery的表變驗證插件中Validation-Engine是一款高質(zhì)量的產(chǎn)品,提示效果非常精美,而且里面包含了AJAX驗證功能2012-06-06
asp.net UrlReWriter使用經(jīng)驗小結(jié)
UrlRewriter 是微軟封裝好了的一個URL重寫組件。使用它可以讓我節(jié)約很多自已開發(fā)的時間。 好了,開始講述我的應用經(jīng)驗,這只是很菜鳥的經(jīng)驗,高手就不用看了。2008-11-11
ASP.NET泛型四之使用Lazy<T>實現(xiàn)延遲加載
這篇文章介紹了ASP.NET泛型使用Lazy<T>實現(xiàn)延遲加載的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
在Asp.net網(wǎng)頁上寫讀Cookie的兩種不同語法介紹
asp.net開發(fā)時,為了存儲一些信息通常是Session與Cookie同時使用,本文將會補充一下Cookie相關(guān)的資料,感興趣的朋友可以了解一下在網(wǎng)頁上寫讀Cookie的實現(xiàn),希望本文對你有所幫助2013-01-01

