3

ビューとプロシージャでSqlCacheDependencyを使用したいと考えています。ビューとプロシージャのSqlCacheDependency

Linq to Sqlを使用しています。

私は現在使用しているコードを使用すると、単一の表を使用している場合にのみ有効です。今私は、ビュー(複数のテーブル)のためsqlcachedependencyを実装する

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; 
    } 
} 

私はこのクエリ

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検索しましたが、私はビューとプロシージャまたは複数のテーブルのために働いたコードを見つけることができません。

答えて

1

テーブルベースの通知を使用する場合、ExecuteNonQueryおよび関連するコードが冗長であるため、別のSqlCommandスタイルのクエリを発行する必要はありません。作成したAggregateCacheDependencyをLINQ結果に追加するだけです。

テーブルベースの変更通知を完全に有効にしてもよろしいですか?テーブルには、DBに作成する必要のあるトリガとストアドプロシージャ、および定期的なポーリングをトリガするサーバー上のコードがあります。キャッシュが期限切れではないと宣言する前に、ポーリングが発生するのに十分な時間待ってください。

あなたはまた、変更がDB側でピックアップされているかどうかを確認するために、変更テーブルの内容で自分を見ることができます:

SELECT * [AspNet_SqlCacheTablesForChangeNotification]

[DBO]からのIF。単一のテーブルからの変更が機能している場合は、代わりにSqlCacheDependency.CreateOutputDependency(文字列依存)を使用して、コードを単純化してみてください。引数は、 "DdName:TableName; DbName:TableName"という形式の1つ以上のテーブルのリストです。 DbNameはweb.configにあります。

これまで述べてきたように、テーブルベースの変更通知は非推奨であり、新しいコードではService Broker通知を使用する必要があります。はい、LINQで作業することができます。

関連する問題