关于SqlCacheDependency缓存机制的使用疑问
这几天在调试依赖缓存的使用,试了两种方式:
1,轮询方式
using (BASEEntities db = new BASEEntities())
{
//如果没有缓存 重新获取数据
var links = db.Promotions.AsQueryable();
//添加到缓存中
HttpRuntime.Cache.Insert(
"Link",
links,
new SqlCacheDependency("DEV_BASE", "Promotions")
);
//返回数据
list = links.ToList();
}
2,通知方式
DataTable dtPromotions = HttpRuntime.Cache["dtPromotions"] as DataTable;
if (dtPromotions == null)
{
Response.Write("no cache <br />");
using (SqlConnection sqlCon = new SqlConnection(CONNSTR))
{
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.CommandText = "select employeeno, employeename from dbo.Promotions";
dtPromotions = new DataTable();
SqlCacheDependency scd = new SqlCacheDependency(sqlCmd);
SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
sda.Fill(dtPromotions);
HttpRuntime.Cache.Insert("dtPromotions", dtPromotions, scd);
sqlCon.Close();
}
}
else
{
Response.Write("read cache <br />");
}
两种方式都调试成功了。但想在通知方式中,使用EF 实体架构来实现,碰到了瓶颈。难道EF只能使用轮询方式吗?求解