缓存过期不回调,这真的让人很郁闷

zyug 2008-01-11 09:48:02
来看一个最简单的
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码

}

void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码

}

void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码

}

void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。

}


static void onRemove(string key, Object item, CacheItemRemovedReason reason)
{


StreamWriter sw = File.AppendText(HttpContext.Current.Server.MapPath(".") + "\\IPRecord.txt");
sw.Write("key:");
sw.Write(key);
sw.Write(" value:");
sw.Write(item.ToString());
sw.Write(" 移除 时间: ");
sw.Write(System.DateTime.Now.ToString());
sw.Write(".");
sw.Write(System.DateTime.Now.Millisecond.ToString());
sw.WriteLine("");
sw.Flush();
sw.Close();

HttpRuntime.Cache.Insert("Key", DateTime.Now, null, DateTime.Now.AddSeconds(10),
Cache.NoSlidingExpiration, CacheItemPriority.Default,
new CacheItemRemovedCallback(onRemove));


}

void Application_Start(Object sender, EventArgs e)
{
StreamWriter sw = File.AppendText(HttpContext.Current.Server.MapPath(".") + "\\IPRecord.txt");
sw.Write("key:");
sw.Write(" Key ");
sw.Write(" value:");
sw.Write(DateTime.Now.ToString());
sw.Write(" 建立 时间: ");
sw.Write(System.DateTime.Now.ToString());
sw.Write(".");
sw.Write(System.DateTime.Now.Millisecond.ToString());
sw.WriteLine("");
sw.Flush();
sw.Close();

HttpRuntime.Cache.Insert("Key", DateTime.Now, null, DateTime.Now.AddSeconds(10),
Cache.NoSlidingExpiration, CacheItemPriority.Default,
new CacheItemRemovedCallback(onRemove));
}


//
运行

结果 IPRecord.txt中就一条,无论多长时间就一条,只有建立时的日志

key: Key value:2008-1-11 9:31:16 建立 时间: 2008-1-11 9:31:16.859


没脾气了.把MSDN的例子拷到vs2005里面,把时间调成10秒过期,方便调试.....

public static class ReportManager
{
private static bool _reportRemovedFromCache = false;
static ReportManager()
{ }

public static String GetReport()
{
lock (typeof(ReportManager))
{
if (HttpContext.Current.Cache["MyReport"] != null)
return (string)HttpRuntime.Cache["MyReport"];
else
{
CacheReport();
return (string)HttpRuntime.Cache["MyReport"];
}
}
}

public static void CacheReport()
{
lock (typeof(ReportManager))
{
HttpContext.Current.Cache.Add("MyReport",
CreateReport(), null, DateTime.Now.AddSeconds(10),
TimeSpan.Zero,
System.Web.Caching.CacheItemPriority.Default,
ReportRemovedCallback);

StreamWriter sw = File.AppendText(HttpContext.Current.Server.MapPath(".") + "\\IPRecord.txt");
//记录建立
sw.Write(" MyReport 创建 时间: ");
sw.Write(System.DateTime.Now.ToString());
sw.Write(".");
sw.Write(System.DateTime.Now.Millisecond.ToString());
sw.WriteLine("");
sw.Flush();
sw.Close();
}
}

private static string CreateReport()
{
System.Text.StringBuilder myReport =
new System.Text.StringBuilder();
myReport.Append("Sales Report<br />");
myReport.Append("2005 Q2 Figures<br />");
myReport.Append("Sales NE Region - $2 million<br />");
myReport.Append("Sales NW Region - $4.5 million<br />");
myReport.Append("Report Generated: " + DateTime.Now.ToString()
+ "<br />");
myReport.Append("Report Removed From Cache: " +
_reportRemovedFromCache.ToString());
return myReport.ToString();
}

public static void ReportRemovedCallback(String key, object value,
CacheItemRemovedReason removedReason)
{
//记录移除
StreamWriter sw = File.AppendText(HttpContext.Current.Server.MapPath(".") + "\\IPRecord.txt");
sw.Write("key:");
sw.Write(key);
sw.Write(" value:");
sw.Write(value.ToString());
sw.Write(" 移除 时间: ");
sw.Write(System.DateTime.Now.ToString());
sw.Write(".");
sw.Write(System.DateTime.Now.Millisecond.ToString());
sw.WriteLine("");
sw.Flush();
sw.Close();

_reportRemovedFromCache = true;
CacheReport();
}
}


结果一样..
还是只能跟踪到缓存建立时的记录

....什么破玩意,真怀疑这些人有没有测试

...全文
285 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
boblaw 2008-01-11
  • 打赏
  • 举报
回复
另,我在6樓發的代碼public static readonly string path可改為private
zyug 2008-01-11
  • 打赏
  • 举报
回复
果真如此..还是黑马兄历害,

一时被气晕,果真是httpcontext.current对像为null

结贴
aofengdaxia 2008-01-11
  • 打赏
  • 举报
回复
帮你顶了
SeerMi 2008-01-11
  • 打赏
  • 举报
回复
不用考虑,来接了
vvsamln 2008-01-11
  • 打赏
  • 举报
回复
貌似你是把日志文件覆盖写入了
boblaw 2008-01-11
  • 打赏
  • 举报
回复
LZ不要激動,把你的global.asax改為如下內容,你再試一下,就會有效果了。
之所以你的衹記錄一次,是因為除了第一次,其他每次執行都出錯了。出錯的問題在於HttpContext.Current值為null

<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">

public static readonly string path = HttpContext.Current.Server.MapPath(".") + "\\IPRecord.txt";
void Application_Start(object sender, EventArgs e)
{
StreamWriter sw = File.AppendText(path);
sw.Write("key:");
sw.Write(" Key ");
sw.Write(" value:");
sw.Write(DateTime.Now.ToString());
sw.Write(" 建立 时间: ");
sw.Write(System.DateTime.Now.ToString());
sw.Write(".");
sw.Write(System.DateTime.Now.Millisecond.ToString());
sw.WriteLine("");
sw.Flush();
sw.Close();

HttpRuntime.Cache.Insert("Key", DateTime.Now, null, DateTime.Now.AddSeconds(10),
Cache.NoSlidingExpiration, CacheItemPriority.Default,
new CacheItemRemovedCallback(onRemove));
}
static void onRemove(string key, Object item, CacheItemRemovedReason reason)
{


StreamWriter sw = File.AppendText(path);

sw.Write("key:");
sw.Write(key);
sw.Write(" value:");
sw.Write(item.ToString());
sw.Write(" 移除 时间: ");
sw.Write(System.DateTime.Now.ToString());
sw.Write(".");
sw.Write(System.DateTime.Now.Millisecond.ToString());
sw.WriteLine("");
sw.Flush();
sw.Close();

HttpRuntime.Cache.Insert("Key", DateTime.Now, null, DateTime.Now.AddSeconds(10),
Cache.NoSlidingExpiration, CacheItemPriority.Default,
new CacheItemRemovedCallback(onRemove));


}

void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码

}

void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码

}

void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码

}

void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。

}
</script>

rangeon 2008-01-11
  • 打赏
  • 举报
回复
帮顶
zhuanshen712 2008-01-11
  • 打赏
  • 举报
回复
那我就顶!
nj_1st_excellence 2008-01-11
  • 打赏
  • 举报
回复
接分哦~
zyug 2008-01-11
  • 打赏
  • 举报
回复
顶者有分,要是闲分少还可以再加
ydlchina 2008-01-11
  • 打赏
  • 举报
回复
顶顶

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧