作者:张洪幸_246 | 来源:互联网 | 2023-05-17 20:55
IamusingSQLCacheDependencyinmyASP.NETapplicationwithQueryNotifications.Ifollowedthisar
I am using SQLCacheDependency
in my ASP.NET
application with Query Notifications
. I followed this article to set up my database with success.However whenever I am trying to store data in the cache
object.It just does not hold value.It is always null
.I am not getting any errors or exceptions. Here is my code
我在我的ASP中使用SQLCacheDependency。带有查询通知的NET应用程序。我根据本文成功地建立了我的数据库。但是,每当我试图在缓存对象中存储数据时。它只是没有价值。它总是null,我没有任何错误或异常。这是我的代码
Global.asax
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;
}
}
Code Behind
后面的代码
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");
}
The above always returns null
.
上面的总是返回null。
Can anyone help me in pointing out what could be missing?
谁能帮我指出可能遗漏了什么吗?
1 个解决方案
3
Well there's a lot you can do to debug your code and arrive at a conclusion. It seems like your cached item is getting removed too frequently.
要调试代码并得出结论,你可以做很多事情。看起来你缓存的条目被删除得太频繁了。
1.) Use CacheItemPriority.NotRemovable
to Cache.Insert() to make sure ASP.NET doesn't removes your item whenever it feels so. use the Insert()
method explained here. Check this MSDN article too.
1)。使用CacheItemPriority。插入()以确保ASP。NET不会删除你的项目,只要它感觉如此。使用这里解释的Insert()方法。请检查此MSDN文章。
2.) To find out the reason why your cached item is getting removed , log this removal action using CacheItemRemovedCallback
delegate option of your Cache.Insert()
method. Check this Insert method overload version and also this link.
2)。要找出缓存项被删除的原因,请使用Cache.Insert()方法的CacheItemRemovedCallback委托选项记录这个删除操作。检查这个插入方法重载版本和这个链接。
3.) Make sure your dtresult
as well as your reader
is not null. Check the lines:
3)。确保你的dtresult和你的阅读器都不是空的。检查线:
SqlDataReader reader = cmd.ExecuteReader();
& dtResult.Load(reader);
, together with your logs.
SqlDataReader读者= cmd.ExecuteReader();& dtResult.Load(读者);,以及你的日志。
4.) Check your application Pool recycle time. This link has everything related to App pool settings ( IIS 7 +).
4)。检查应用程序池的回收时间。这个链接与应用程序池设置相关(IIS 7 +)。
5.) This link has a solution for App pool of IIS 6: http://bytes.com/topic/net/answers/717129-c-asp-net-page-cache-getting-removed-too-soon
5)。本链接针对IIS 6的应用程序池提供了一个解决方案:http://bytes.com/topic/net/answers/717129-c-asp-net-page-cache- removed-too-soon
Also, try using HttpRuntime.Cache method to see if it works.
同时,试着用HttpRuntime。缓存方法,看看它是否有效。
System.Web.HttpRuntime.Cache.Insert(cacheName, dtResult, dependency);