2013-08-30 64 views
3

我在使用SQLCacheDependencyASP.NET應用程序中使用了Query Notifications。 我跟着this article成功建立我的數據庫。但是每當我試圖存儲數據在cache object.It只是沒有價值。它總是null。我沒有得到任何錯誤或例外。 這裏是我的代碼ASP.NET緩存始終返回空值

的Global.asax

void Application_Start(object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     System.Data.SqlClient.SqlDependency. 
     Start(ConfigurationManager.ConnectionStrings["McdConn"].ToString()); 

    } 

void Application_End(object sender, EventArgs e) 
    { 
     // Code that runs on application shutdown 
     System.Data.SqlClient.SqlDependency. 
     Stop(ConfigurationManager.ConnectionStrings["McdConn"].ToString()); 
    } 

public static class CacheManagement 
{ 
    public static DataTable CreateCache(string cacheName, string tableName, string query) 
    { 
     DataTable dtResult = new DataTable(); 
     try 
     { 
      string connectionString = ConfigurationManager.ConnectionStrings["McdConn"].ToString(); 
      dtResult = HttpContext.Current.Cache[cacheName] as DataTable; 

      if (dtResult == null) 
      { 
       dtResult = new DataTable(); 
       using (var cn = new SqlConnection(connectionString)) 
       { 
        cn.Open(); 
        var cmd = new SqlCommand(query, cn); 
        cmd.Notification = null; 
        cmd.NotificationAutoEnlist = true; 
        SqlCacheDependencyAdmin.EnableNotifications(connectionString); 
        if (!SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connectionString).Contains(tableName)) 
        { 
         SqlCacheDependencyAdmin.EnableTableForNotifications(connectionString,tableName); 
        } 

        var dependency = new SqlCacheDependency(cmd); 
        //SqlDataAdapter ad = new SqlDataAdapter(cmd); 
        //ad.Fill(dsResult); 
        SqlDataReader reader = cmd.ExecuteReader(); 
        dtResult.Load(reader); 
        HttpContext.Current.Cache.Insert(cacheName, dtResult, dependency); 

       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Exception_Log.ExceptionMethod("Web", "CacheManagement.cs", "CacheManagement", ex); 
     } 

     return dtResult = HttpContext.Current.Cache[cacheName] as DataTable; 
    } 

} 

代碼隱藏

var dtCachedCategories = HttpContext.Current.Cache["tbl_CategoryMaster_Cached"] as DataTable; 
if (dtCachedCategories == null) 
{ 
dtCachedCategories = CacheManagement.CreateCache("tbl_CategoryMaster_Cached","dbo.tbl_CategoryMaster_Languages", "Select * from dbo.tbl_CategoryMaster_Languages"); 

} 

上面總是返回null

任何人都可以幫我指出可能會丟失什麼?

+0

您能否提供有關您的問題跟蹤的更多信息?執行插入時dtResult是否爲空?嘗試沒有查詢通知時它會改變什麼嗎?與原子類型值? – jbl

+0

@jbl從緩存中檢索時,'dtResult'總是空的。我認爲Cache永遠不會保存'dtResult'。 – freebird

回答

3

那麼有很多你可以做的調試你的代碼並得出結論。看起來你的緩存項目被頻繁刪除。

1.)使用CacheItemPriority.NotRemovable Cache.Insert()以確保ASP.NET不會刪除 您的項目,只要它的感覺如此。使用Insert()方法解釋here。文章也檢查這MSDN 文章。

2.)要找出您的緩存項目被刪除的原因,請使用Cache.Insert()方法的 CacheItemRemovedCallback代理選項記錄此刪除操作。請檢查此Insert method overload版本以及此link

3.)確保您的dtresult以及reader不爲空。檢查行:

SqlDataReader reader = cmd.ExecuteReader(); & dtResult.Load(reader);,連同您的日誌。

4.)檢查您的應用程序池回收時間。此link包含與應用程序池設置(IIS 7 +)相關的​​所有內容。

5)此連結IIS 6的應用程序池的解決方案:http://bytes.com/topic/net/answers/717129-c-asp-net-page-cache-getting-removed-too-soon

而且,嘗試使用HttpRuntime.Cache方法,看看它是否工作。

System.Web.HttpRuntime.Cache.Insert(cacheName, dtResult, dependency);