Httpruntime 缓存键不唯一?
虽然我已经指定了一个唯一的键,但似乎以下代码将为 5 个请求返回一个值,然后为接下来的几个请求返回另一个值,然后恢复到原始请求中保存的值,然后继续,直到有 10 个不同的对象全部存在存储在同一密钥下。 那么将从缓存中返回哪些值似乎几乎是随机的。
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);
很困惑,这是因为线程还是什么?
Although i have specified a unique key, it seems the following code will return one value for 5 requests, then another for the next couple, then revert back to the value saved in the original request and just continue until there are 10's of different objects all stored under the same key.
It then seems almost random which of these values it will return from the cache.
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);
Very confused, is this because of threading or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
忽略服务器场和负载平衡的可能性,此行为可能是由作为 网络花园。引用MSDN的相关部分:
这将导致它看起来好像缓存正在为同一键存储多个值,从而实际上在缓存中具有重复的条目。
要在 IIS 7 中解决此问题,请打开应用程序池的“高级设置”并将“最大工作进程数”设置为 1。对于 IIS 6,请参阅MSDN 文章(带有漂亮的屏幕截图) 。
尽管晚了 8 个月,我还是回答这个问题,因为我早在发现 这篇关于网络花园陷阱的不错的文章。希望这个答案能为未来的搜索者节省大量时间。 :)
Ignoring the possibility of a server farm and load balancing, this behaviour can be caused by the application pool running as a web-garden. To quote the relevant section from MSDN:
This will cause it to appear as if caching is storing multiple values for the same key, effectively having duplicate entries in the cache.
To resolve this in IIS 7, open the application pool's Advanced Settings and set Maximum Worker Processes to 1. For IIS 6, see the MSDN article (With pretty screenshots).
Albeit 8 months late, I'm answering this question because I found it long before I found this decent article on web-garden gotchas. Hopefully this answer will save future searchers a chunk of time. :)
您的缓存键始终为“datetimeexample”,因此,缓存中始终有一个对象;并且您将永远收到该物品。
我不太确定你想在这里完成什么,就我而言,这完全按照它应该做的方式进行。
Your cachekey is always 'datetimeexample', therefore, you will always have one object in cache; and you will always receive that object back.
I am not quite sure what you are trying to accomplish here, as far as I'm concerned, this behaves exactly in the way it's supposed to do.