【发布时间】:2011-03-14 14:02:01
【问题描述】:
我已经实现了 asp.net 缓存。但我得到了奇怪的结果
与大多数缓存不同,您试图避免对数据库的点击量。我试图避免用户对数据库的任何点击。这是第一个页面加载所需的时间。它基本上是一个包含大量图表和长时间运行查询的仪表板
我尝试了几种技术 1)缓存时间很长,有一个调度进程过期并获得新的缓存。 2) 并在 RemoveCallback 上
在第二个选项中,我让所有缓存都通过我创建的静态类。目的是在过期时刷新数据。这就是我所说的。
Cache.Insert(dbCacheString, dtNetwork, null, DateTime.Now.AddHours(2),
System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High,
new CacheItemRemovedCallback(CacheManager.CacheRemovedCallback));
public static class CacheManager
{
private static Hashtable times = new Hashtable();
private static bool isRefreshingCache = false;
public static void CacheRemovedCallback(String key, object value,
CacheItemRemovedReason removedReason)
{
RefreshCache(key);
}
public static void StartCache()
{
string lcUrl = "http://localhost/ratingspage/";
// *** Establish the request
try
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("xxx", "xxx",
"xxx");
byte[] myDataBuffer = client.DownloadData(lcUrl);
}
catch (Exception ex)
{
ErrHandler.WriteError(ex.Message + "\n" +
ex.StackTrace.ToString());
LogUtil.LogDebugMessages(ex.Message + ":" +
ex.StackTrace.ToString());
}
}
public static void RefreshCache(string key)
{
string controlname = "";
if ( key.ToLower().StartsWith("control:") )
{
string[] tmp = key.Split(':');
if (tmp.Length > 1)
controlname = tmp[1];
else
return;
}
else
return;
string lcUrl = "http://localhost/ratingspage/Admin/" + "
"LoadControl.aspx?CachingSpider=true&Control=" + controlname;
string lcHtml = isRefreshingCache.ToString();
// *** Establish the request
if (!isRefreshingCache)
{
isRefreshingCache = true;
lcHtml = isRefreshingCache.ToString();
try
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("xxx",
"xxx", "xxx");
byte[] myDataBuffer = client.DownloadData(lcUrl);
lcHtml = Encoding.ASCII.GetString(myDataBuffer);
}
catch (Exception ex)
{
lcHtml = ex.Message;
isRefreshingCache = false;
ErrHandler.WriteError(ex.Message + "\n" +
ex.StackTrace.ToString());
LogUtil.LogDebugMessages(ex.Message + ":" +
ex.StackTrace.ToString());
}
isRefreshingCache = false;
}
MailMessage mail = new MailMessage(
new MailAddress("jgiblin@univision.net"),
new MailAddress("jgiblin@univision.net"));
mail.Subject = "Cache Expire: " + key;
mail.Body = string.Format("The Key {0} has expired at {1}",
key, DateTime.Now.ToShortDateString() + " " +
DateTime.Now.ToShortTimeString()) + "\nRefreshing Cache: " +
lcHtml;
SmtpClient smtp = new SmtpClient("mercury.utg.uvn.net");
mail.IsBodyHtml = false;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
ErrHandler.WriteError(ex.Message + "\n" +
ex.StackTrace.ToString());
LogUtil.LogDebugMessages(ex.Message + ":" +
ex.StackTrace.ToString());
}
}
}
由于某种原因,当我转到该页面时。有时数据会被缓存,有时则不会。这里有什么问题吗
我尝试了应用程序结构,但由于服务器没有 iis 7,我无法使用它
【问题讨论】:
-
您确定没有在两次访问之间重新启动 appdomain(即按 F5 进行调试会清除缓存)。
-
只是一些建议,我认为您可以通过不将应用程序代码与缓存管理器交织在一起来改善这一点。 CacheManager 应该只在您使用的任何底层缓存系统(redis、其他)中获取/设置键/值。在您现有的业务逻辑中,例如获取用户,您检查缓存,如果它为空,您从数据库中检索,放入缓存然后返回项目。如果缓存确实包含用户,那么您只需从缓存中返回用户并跳过您的数据库查询。