.net

先看下MSDN对此类的介绍:

在以下两者之间建立关系:一是在 ASP.NET 应用程序的 Cache 对象中存储的项;二是特定 SQL Server 数据库表或 SQL Server 2005 查询的结果。无法继承此类。

命名空间:System.Web.Caching
程序集:System.Web(在 system.web.dll 中)





这是我作的一个例子:

新建一个页面CacheSqlDependency.aspx,加入如下代码
+ expand sourceview plaincopy to clipboardprint?protected void Page_Load(object sender, EventArgs e) { DataTable dt = GetTableFromDB(); dt.TableName = "CacheDependency"; TextWriter writer = new StringWriter(); dt.WriteXml(writer); Response.Write(writer.ToString()); } private DataTable GetTableFromDB() { DataTable result = Cache.Get("cacheStudy") as DataTable; if(result != null) return result; using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ForStudy"].ConnectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("select * from CacheDependency")) { cmd.Connection = conn; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd; result = new DataTable(); adapter.Fill(result); SqlCacheDependency dependency = new SqlCacheDependency("ForStudy", "CacheDependency"); Cache.Add("cacheStudy", result, dependency, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, null); return result; } } } protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = GetTableFromDB();
dt.TableName = "CacheDependency";
TextWriter writer = new StringWriter();
dt.WriteXml(writer);
Response.Write(writer.ToString());
}

private DataTable GetTableFromDB()
{
DataTable result = Cache.Get("cacheStudy") as DataTable;
if(result != null) return result;

using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ForStudy"].ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("select * from CacheDependency"))
{
cmd.Connection = conn;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
result = new DataTable();
adapter.Fill(result);

SqlCacheDependency dependency = new SqlCacheDependency("ForStudy", "CacheDependency");
Cache.Add("cacheStudy", result, dependency, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, null);
return result;
}
}
}
WebConfig中增加SqlCacheDependency的配置
view plaincopy to clipboardprint?<caching> <sqlCacheDependency enabled="true" pollTime="60000"> <databases> <add name="ForStudy" connectionStringName="ForStudy" pollTime="60000"/> </databases> </sqlCacheDependency> </caching> <caching>
<sqlCacheDependency enabled="true" pollTime="60000">
<databases>
<add name="ForStudy" connectionStringName="ForStudy" pollTime="60000"/>
</databases>
</sqlCacheDependency>
</caching>
开启缓存通知
aspnet_regsql.exe -S ADIMIN-B7575567/SQLEXPRESS -U sa -P sa -ed -d ForStudy -et -t CacheDependency
开启对数据库ForStudy表CacheDependency表的缓存依赖,如果不开启,程序则会报异常:没有开启数据库XXX的缓存依赖,没有开启数据表XXX的缓存依赖
WebConfig配置
<connectionStrings > <add name="ForStudy" connectionString="Data Source=ADIMIN-B7575567/SQLEXPRESS;Initial Catalog=ForStudy;Integrated Security=True"/> </connectionStrings>
这是这个例子中用到的数据库连接
开始测试
数据库中执行
use forstudy
create table CacheDependency
(
id int identity,
desp nvarchar(4000)
)
insert into CacheDependency
values ('一')
第一次访问界面显示:
from db
1 一

在数据库中执行:insert into CacheDependency values ('二')
刷新页面显示:
from cache
1 一
一分钟后再次刷新页面显示:
from cache
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值