本文實例講述了asp.net中SqlCacheDependency緩存技術,對于大型web程序設計來說具有很高的實用價值。具體如下:
對于訪問量大,但更新較少的網(wǎng)站中使用緩存技術,可以大大提高運行效率;加上.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í)行相關的配置步驟。有兩種方法配置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集成驗證方式。我們還可以對相關的數(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;
}
希望本文所述緩存技術對大家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