1.首先开通此功能
若要设置 SQL 缓存依赖项,您需要具有管理特权,或管理帐户和密码。下面的命令为 Northwind 数据库中的 Employees 表启用 SQL 缓存依赖项。
(运行:C:/WINDOWS/Microsoft.NET/Framework/<versionNumber>/aspnet_regsql.exe)
aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d Northwind -et -t Employees
在数据库中会看到此表:AspNet_SqlCacheTablesForChangeNotification 是专门用来记录高速缓存禁用的数据表的情况的。
2.web.config中添加以下内容
<caching>
<sqlCacheDependency enabled="true">
<databases>
<add name="CacheData" connectionStringName="CacheDataConnectionString" pollTime="500"></add>
</databases>
</sqlCacheDependency>
</caching>
如果使用了sql server2005的通知功能,就不需要pollTime属性。
3.具体的使用方法(包括页面级的和程序级的)
-----------------------------------------------------------------------------------
一。页面级的高速缓存禁用功能。
.aspx页面需要添加如下内容:
<%@ OutputCache Duration="3600" VaryByParam="none" SqlDependency="数据库:表名" %>
.aspx.cs页面内容:
using System.Web.Caching;
using System.Web;
protected void Page_Load(object sender, EventArgs e)
{
DataSet myCustomers;
myCustomers = (DataSet)Cache["firmCustomers"];
if (myCustomers == null)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CacheDataConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from news", conn);
myCustomers = new DataSet();
da.Fill(myCustomers);
SqlCacheDependency myDependency = new SqlCacheDependency("CacheData", "news");
Cache.Insert("firmCustomers", myCustomers, myDependency);
this.Label1.Text = "来源于数据库";
}
else
{
this.Label1.Text = "来源于缓存";
}
this.GridView1.DataSource = myCustomers;
this.GridView1.DataBind();
}
-----------------------------------------------------------------------------------
二。程序级的高速缓存禁用功能。
缓存实体类:
public class CacheMod
{
private string _Key;
private string _Source;
//缓存的Key值
public string Key
{
get { return _Key; }
set { _Key = value; }
}
//缓存的来源
public string Source
{
get { return _Source; }
set { _Source = value; }
}
//构造函数
public CacheMod(string strKey,string strSource)
{
_Key = strKey;
_Source = strSource;
}
public CacheMod()
{ }
}
注意:数据访问层需要添加引用System.Web
获取缓存的通用类(数据访问层)
using System.Web;
using System.Web.Caching;
/// <summary>
/// 获取缓存数据
/// </summary>
/// <param name="cacheData">缓存的数据库</param>
/// <param name="cacheTable">缓存的数据表</param>
/// <param name="cacheKey">缓存的Key值</param>
/// <param name="cacheSource">缓存的数据填充值</param>
public void GetCache(string cacheData, string cacheTable, string cacheKey, object cacheSource)
{
SqlCacheDependency myDependency = new SqlCacheDependency(cacheData, cacheTable);
HttpRuntime.Cache.Insert(cacheKey, cacheSource, myDependency);
}
需要缓存的数据类:(数据访问层)
using System.Web.Caching;
using System.Web;
/// <summary>
/// 返回dataset数据集
/// </summary>
/// <returns></returns>
public DataSet GetNews(CacheMod RequestCache)
{
if (HttpRuntime.Cache[RequestCache.Key] == null)
{
string sql = "select * from news";
DataSet ds = SqlHelper.GetDataSet(SqlHelper.conn_Default, CommandType.Text, sql, null);
CacheFactory cf = new CacheFactory();
cf.GetCache("CacheData", "news", RequestCache.Key, ds);
RequestCache.Source = "Database";
return ds;
}
else
{
RequestCache.Source = "Cache";
return (DataSet)HttpRuntime.Cache[RequestCache.Key];
}
}
.aspx.cs 页面程序:
protected void Page_Load(object sender, EventArgs e)
{
News news=new News();
CacheMod re = new CacheMod("mynews", "未知");
this.GridView1.DataSource = news.GetNews(re);
this.GridView1.DataBind();
this.Label1.Text = re.Source;
}
总结:推荐使用程序级的高速缓存功能,通用性较强。