尽管我已经指定了一个唯一的键,但似乎下面的代码将为5个请求返回一个值,然后再为另一个返回另一个值,然后恢复到原始请求中保存的值并继续,直到有10个不同的值对象全部存储在相同的密钥下。 它似乎几乎随机哪些值将从缓存中返回。Httpruntime缓存键不是唯一的吗?
string strDateTime = string.Empty;
string cachename = "datetimeexample";
object cachedobject = HttpRuntime.Cache.Get(cachename);
if (cachedobject != null)
strDateTime = (string)cachedobject;
else
{
strDateTime = DateTime.Now.ToString();
HttpRuntime.Cache.Insert(cachename, strDateTime, null, DateTime.MaxValue, TimeSpan.FromDays(10), CacheItemPriority.NotRemovable, null);
}
Response.Write(strDateTime +" keys:"+ HttpRuntime.Cache.Count);
非常困惑,这是因为线程还是什么?
你确定它存储多个对象具有相同的密钥?也就是说,你确定钥匙*完全相同吗?如果键已经存在,'Insert'方法应该覆盖现有的对象。 – LukeH 2009-09-14 12:04:03
是的,关键是完全一样的,代码片断包装在静态方法/类中的图像。 – maxp 2009-09-14 12:46:52
您正在单台机器或服务器场上运行吗? – LukeH 2009-09-15 10:06:18