C#緩存之SqlCacheDependency用法實(shí)例總結(jié)
本文整理匯總了C#緩存的數(shù)據(jù)庫(kù)依賴類SqlCacheDependency的使用方法,具體內(nèi)容如下:
1、數(shù)據(jù)庫(kù)依賴類SqlCacheDependency
數(shù)據(jù)庫(kù)緩存依賴主要解決的是當(dāng)數(shù)據(jù)庫(kù)的內(nèi)容發(fā)生改變時(shí),如何及時(shí)通知緩存,并更新緩存中的數(shù)據(jù)的問題。
語(yǔ)法定義:
SqlCacheDependency類主要的構(gòu)造函數(shù)如下:
public SqlCacheDependency(string database,string table)
其中參數(shù)一代表要啟用緩存的數(shù)據(jù)庫(kù),參數(shù)二表示緩存的表。在實(shí)際使用過程中,只需要指明緩存的數(shù)據(jù)庫(kù)和表即可。
方法是屬性的應(yīng)用(代碼與CacheDependency類似),不過Sql需要先進(jìn)行一下對(duì)web.config進(jìn)行配置和設(shè)置數(shù)據(jù)庫(kù)的緩存配置一下才可以使用SqlCacheDependency緩存類
首先web.config配置如下:
<!--連接數(shù)據(jù)庫(kù)語(yǔ)句--> <configuration> <connectionStrings> <add name="Config" connectionString="Data Source=.;Initial Catalog=CacheData;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/> </connectionStrings> <!--在system.web節(jié)點(diǎn)下添加--> <!--注意事項(xiàng):配置中add name值為數(shù)據(jù)庫(kù)名,connectionStringName為連接數(shù)據(jù)庫(kù)字段的名稱要相同--> <caching> <sqlCacheDependency enabled="true" pollTime="1000"> <databases> <add name="CacheData" connectionStringName="Config" pollTime="1000"/> </databases> </sqlCacheDependency> </caching>
2、Vs緩存配置:
打開“開始”|“所有程序”|“Microsoft Visual Studio 2010”|“Visual Studio Tools”|“Visual Studio 2010命名提示”菜單命令。
在命令框內(nèi)輸入:aspnet_regsql.exe -S SqlServer服務(wù)器 -U <Username> -P <Password> -ed -d 數(shù)據(jù)庫(kù)名稱 -et -t 表名
若無身份驗(yàn)證輸入:aspnet_regsql.exe -S SqlServer服務(wù)器 -ed -d 數(shù)據(jù)庫(kù)名稱 -et -t 表名
執(zhí)行命令即可;
3、頁(yè)面代碼;
private static SqlCacheDependency MyDep;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
if (!IsPostBack)
{
//Cache為數(shù)據(jù)庫(kù)名,T_SqlCache為緩存表
DataSet ds = GetSet();
if (Cache["SqlCon"] == null)
{
//添加緩存SqlCon,緩存值為數(shù)據(jù)庫(kù)表內(nèi)容,
MyDep = new SqlCacheDependency("Cache", "T_SqlCache");
Cache.Add("SqlCon", ds, MyDep, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.Normal, null);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (MyDep.HasChanged)
{//當(dāng)數(shù)據(jù)庫(kù)值更改時(shí)提醒;
Response.Write("數(shù)據(jù)庫(kù)修改時(shí)間為:"+MyDep.UtcLastModified);
}
if (Cache["SqlCon"] == null)
{//當(dāng)緩存過期或數(shù)據(jù)庫(kù)值修改后緩存從新加載
MyDep = new SqlCacheDependency("Ajax", "T_AjaxLD");
DataSet ds = GetSet();
Cache.Add("SqlCon", ds, MyDep, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.Normal, null);
}
this.GridView1.DataSource = Cache["SqlCon"];//綁定數(shù)據(jù)
this.GridView1.DataBind();
}
/// <summary>
/// 生成Dataset
/// </summary>
/// <returns></returns>
private DataSet GetSet()
{
DataSet ds = new DataSet();
string sql = "select * from T_SqlCache";
string Config = ConfigurationManager.ConnectionStrings["Config"].ConnectionString;//連接數(shù)據(jù)庫(kù)語(yǔ)句
using (SqlConnection cnn = new SqlConnection(Config))
{
using (SqlCommand cmm = new SqlCommand(sql, cnn))
{
SqlDataAdapter dapter = new SqlDataAdapter(cmm);
dapter.Fill(ds);
}
}
return ds;
}
C#緩存基本內(nèi)容就差不多這些,一些應(yīng)用需要在實(shí)踐中總結(jié)出來,此處順便分析一下session和Cache的區(qū)別:
Session和Cache的區(qū)別:
以前實(shí)現(xiàn)數(shù)據(jù)的緩存有很多種方法,有客戶端的Cookie,有服務(wù)器端的Session和Application。其中Cookie是保存在客戶端的一組數(shù)據(jù),主要用來保存用戶名等個(gè)人信息。Session則保存對(duì)話信息。Application則是保存在整個(gè)應(yīng)用程序范圍內(nèi)的信息,相當(dāng)于全局變量。通常使用最頻繁的是Session,那么Session和Cache又有什么區(qū)別呢?
本節(jié)結(jié)合使用經(jīng)驗(yàn),詳細(xì)介紹Session緩存和Cache緩存的區(qū)別如下:
(1)最大的區(qū)別是Cache提供緩存依賴來更新數(shù)據(jù),而Session只能依靠定義的緩存時(shí)間來判斷緩存數(shù)據(jù)是否有效。
(2)即使應(yīng)用程序終止,只要Cache.Add方法中定義的緩存時(shí)間未過期,下次開啟應(yīng)用程序時(shí),緩存的數(shù)據(jù)依然存在。而Session緩存只是存在于一次會(huì)話中,會(huì)話結(jié)束后,數(shù)據(jù)也就失效了。
(3)Session容易丟失,導(dǎo)致數(shù)據(jù)的不確定性,而Cache不會(huì)出現(xiàn)這種情況。
(4)由于Session是每次會(huì)話就被加載,所以不適宜存放大量信息,否則會(huì)導(dǎo)致服務(wù)器的性能降低。而Cache則主要用來保存大容量信息,如數(shù)據(jù)庫(kù)中的多個(gè)表。
(5)VS2005的測(cè)試版提供了將緩存保存在硬盤上的參數(shù),但正式版中取消了這個(gè)功能,估計(jì)其在以后版本中會(huì)重新實(shí)現(xiàn)。而Session目前只能保存在內(nèi)存中,對(duì)其性能有影響。
此外,需要特別注意:為了提高Cache的有效利用率,建議對(duì)于不經(jīng)常改動(dòng)的數(shù)據(jù)使用Cache。
相關(guān)文章
利用Aspose.Cells和Excel模板導(dǎo)出統(tǒng)計(jì)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了利用Aspose.Cells和Excel模板導(dǎo)出復(fù)雜的統(tǒng)計(jì)數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Unity學(xué)習(xí)之FSM有限狀態(tài)機(jī)
這篇文章主要介紹了Unity學(xué)習(xí)之FSM有限狀態(tài)機(jī),通過詳細(xì)的代碼案例來進(jìn)行解析說明,希望這篇文章對(duì)你有所幫助2021-06-06
C#實(shí)現(xiàn)窗體抖動(dòng)的兩種方法
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)窗體抖動(dòng)的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11

