我想在视图和过程中使用SqlCacheDependency。
我正在使用Linq到Sql。
只有在使用单个表时,我当前使用的代码才有效:
public static List<T> LinqCache<T>(this System.Linq.IQueryable<T> q, System.Data.Linq.DataContext dc, string CacheId)
{
try
{
List<T> objCache = (List<T>)System.Web.HttpRuntime.Cache.Get(CacheId);
if (objCache == null)
{
/////////No cache... implement new SqlCacheDependeny//////////
//1. Get connstring from DataContext
string connStr = dc.Connection.ConnectionString;
var c = System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr);
//2. Get SqlCommand from DataContext and the LinqQuery
string sqlCmd = dc.GetCommand(q).CommandText;
//3. Create Conn to use in SqlCacheDependency
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
{
conn.Open();
//4. Create Command to use in SqlCacheDependency
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
{
//5.0 Add all parameters provided by the Linq Query
foreach (System.Data.Common.DbParameter dbp in dc.GetCommand(q).Parameters)
{
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter(dbp.ParameterName, dbp.Value));
}
//5.1 Enable DB for Notifications... Only needed once per DB...
System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
//5.2 Get ElementType for the query
string NotificationTable = q.ElementType.Name;
//5.3 Enable the elementtype for notification (if not done!)
if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable))
System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable);
//6. Create SqlCacheDependency
System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
// - removed 090506 - 7. Refresh the LinqQuery from DB so that we will not use the current Linq cache
// - removed 090506 - dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
//8. Execute SqlCacheDepency query...
cmd.ExecuteNonQuery();
//9. Execute LINQ-query to have something to cache...
objCache = q.ToList();
//10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more...
System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
}
}
}
//Return the created (or cached) List
return objCache;
}
catch (Exception ex)
{
throw ex;
}
}现在,我想实现视图的multiple依赖项(多个表)。
我试着使用这个查询
System.Web.Caching.SqlCacheDependency
dep1 = new System.Web.Caching.SqlCacheDependency(cmd1),
dep2 = new System.Web.Caching.SqlCacheDependency(cmd2);
System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1, dep2);
dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q);
cmd.ExecuteNonQuery();
objCache = q.ToList();
System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, aggDep);但是这个查询不能工作,因为即使我更改了底层表,缓存也不会变得无效。
我在谷歌上搜索了很长时间,但找不到适用于视图和过程或多个表的代码。
发布于 2011-12-03 00:38:36
在使用基于表的通知时,不需要发出单独的SqlCommand样式的查询--您的ExecuteNonQuery和相关代码是多余的。只需将LINQ结果添加到您构建的AggregateCacheDependency中即可。
您确定已经完全启用了基于表的更改通知吗?需要在DB中创建一个表、一些触发器和一个存储过程,再加上服务器上的一些代码来触发周期性轮询。在声明缓存未过期之前,一定要等待足够长的时间才能进行轮询。
您还可以亲自查看更改表的内容,以查看是否在DB端获取更改:
从dbo.AspNet_SqlCacheTablesForChangeNotification中选择*
如果来自单个表的更改有效,您可以尝试使用SqlCacheDependency.CreateOutputDependency(string依赖)来简化代码。参数是表单"DdName:TableName;DbName:TableName“的一个或多个表的列表。DbName来自于web.config。
尽管如此,我还应该指出,不推荐基于表的更改通知,新代码应该使用Service通知。是的,他们可以和LINQ一起工作。
https://stackoverflow.com/questions/8117758
复制相似问题