asp.net - Httpruntime 缓存键不唯一?

标签 asp.net caching unique

虽然我已经指定了一个唯一的键,但似乎以下代码将为 5 个请求返回一个值,然后为接下来的几个请求返回另一个值,然后恢复到原始请求中保存的值并继续,直到有 10 个请求不同的对象都存储在同一个 key 下。 然后,将从缓存中返回哪些值似乎几乎是随机的。

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);

很困惑,这是因为线程还是什么?

最佳答案

忽略服务器场和负载平衡的可能性,此行为可能是由作为 web-garden 运行的应用程序池引起的。 。引用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.

这将导致它看起来好像缓存正在为同一键存储多个值,实际上在缓存中具有重复的条目。

要在 IIS 7 中解决此问题,请打开应用程序池的“高级设置”并将“最大工作进程数”设置为 1。对于 IIS 6,请参阅 MSDN article (带有漂亮的屏幕截图)。

虽然晚了 8 个月,但我还是回答这个问题,因为我早在发现 this decent article on web-garden gotchas 之前就发现了它。 。希望这个答案能为 future 的搜索者节省大量时间。 :)

关于asp.net - Httpruntime 缓存键不唯一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1421070/

相关文章:

PHP mySQL - 选择未从不同表中使用的唯一值

php - "Unique 7 character string"- 相关问题

c# - RedirectToRoute 不会转到 Controller

jquery - 是否可以更改 JSON 中的页面 webmethod 响应,删除 .d 节点?

java - 将值放入谷歌 Guava loadingCache中

caching - Varnish 使用 Magento 1.8.1 社区版未获取缓存结果

ruby-on-rails - rails : how to get all key-values from Rails. 缓存

android - insertOrThrow 如何与 UNIQUE 约束一起使用?

asp.net - 在代码隐藏中创建下拉列表时,不会触发 SelectedIndexChanged 事件

asp.net - 报告服务 2012 - ReportViewer