Httpruntime 缓存键不唯一?

发布于 2024-08-04 21:05:48 字数 637 浏览 3 评论 0原文

虽然我已经指定了一个唯一的键,但似乎以下代码将为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-08-11 21:05:48

忽略服务器场和负载平衡的可能性,此行为可能是由作为 网络花园。引用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:

Because Web gardens enable the use of
multiple processes, each process will
have its own copy of application
state, in-process session state,
caches, and static data. Web gardens
should not be used for all
applications, especially if they need
to maintain state. Be sure to
benchmark the performance of the
application before deciding whether
Web garden mode is appropriate.

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. :)

阳光①夏 2024-08-11 21:05:48

您的缓存键始终为“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文