asp.net - SqlCacheDependency在 View 和过程上

标签 asp.net sql-server-2008 caching sqlcachedependency broker

我想将SqlCacheDependency与 View 和过程一起使用。

我正在使用 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;
   }
}

现在,我想为 View (多个表)实现sqlcachedependency。

我尝试使用此查询
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);

但是此查询不起作用,因为即使我更改了基础表,缓存也不会变得无效。

我在Google上搜索了太长时间,但找不到适合 View 和过程或多个表的代码。

最佳答案

使用基于表的通知时,您不需要发出单独的SqlCommand风格的查询-ExecuteNonQuery和相关代码是多余的。只需将LINQ结果与您构建的AggregateCacheDependency相加即可。

您确定已完全启用基于表的更改通知吗?需要在数据库中创建一个表,一些触发器和一个存储过程,以及服务器上的一些代码以触发定期轮询。在声明缓存未过期之前,请确保等待足够长的时间以进行轮询。

您还可以自己查看更改表的内容,以查看是否在数据库端进行了更改:

SELECT * FROM [dbo]。[AspNet_SqlCacheTablesForChangeNotification]

如果对单个表所做的更改有效,则可以尝试使用SqlCacheDependency.CreateOutputDependency(stringdependent)来简化代码。该参数是一个或多个表的列表,格式为“DdName:TableName; DbName:TableName”。 DbName来自web.config。

说了这么多,我还应该指出,不建议使用基于表的更改通知,而新代码应改为使用Service Broker通知。是的,它们可以与LINQ一起使用。

关于asp.net - SqlCacheDependency在 View 和过程上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8117758/

相关文章:

asp.net - 基于浏览器(免费使用)的 SSH

sql - 无法截断表

sql - 我可以在 SQL Server 上进行实时查询吗?

java - 你将如何在 Java 中实现 LRU 缓存?

android - 缓存 Android 应用程序数据,始终使用 getFilesDir() 是否安全?

c# - asp.net C# 中的非重复随机数

android - java.lang.OutOfMemoryError : (Heap Size=17863KB, 分配=11272KB,位图大小=12444KB)

laravel - 文件 css/all.css 未在 Assets list 中定义

c# - NOSQL数据库选型论坛

sql-server - 将字符串拆分为sql server中的行