2012-07-24 87 views
3

我有一个线程运行在我的ASP.Net后面。在这个线程我把数据在这样的高速缓存:从其他线程访问HttpRuntime.Cache

HttpRuntime.Cache.Insert("test", "test", null, DateTime.Today.AddHours(6), Cache.NoSlidingExpiration); 

在其他线程(网页),我第一次检查,如果缓存中包含的所有数据,然后尝试从缓存中获取对象,像这样:

if (HttpRuntime.Cache.Count > 0) { 
      var test = (string)HttpRuntime.Cache["test"]; 
} 

编辑:当我试图做var test = (string)HttpRuntime.Cache["test"];缓存将走空每次(或将要删除的对象,没有测试缓存多个对象),再加上var test也为空。这是ofcourse当HttpRuntime.Cache.Count比0

哦更大,它使没有例外或任何

+0

你能澄清你是说在你的if语句'Cache.Count> 0'中,但有时在'var test =(string)HttpRuntime.Cache [“test”];'它是空的? – 2012-07-24 15:59:55

+0

每次当我试图做'var test =(string)HttpRuntime.Cache [“test”];'缓存将变空(或将删除对象,还没有测试过)加var test '也是空的。当'HttpRuntime.Cache.Count'大于0时,这是当然的 – 2012-07-24 16:57:59

回答

1

。在你的代码中潜在的不一致是DateTime.Today.AddHours(6)即会不行。你应该使用DateTime.Now.AddHours(6)

DateTime.Today是当天开始​​,如果你的代码运行后6:00 AM明显的httpRuntime缓存不可用。