C#HttpRuntime.Cache.Insert()未持有缓存值 [英] C# HttpRuntime.Cache.Insert() Not holding cached value

查看:1840
本文介绍了C#HttpRuntime.Cache.Insert()未持有缓存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用缓存HttpRuntime.Cache.Insert()的价格值,但只出现在清除之前举行了几个小时或东西的价值。我究竟做错了什么?我想要的值留在缓存中3天。

  HttpRuntime.Cache.Insert(CacheName,价钱,空,DateTime.Now.AddDays(3),TimeSpan.Zero);


解决方案

简短的回答

您的应用程序池或网站被关闭太快。扩展网站上的空闲超时,延长应用程序池生存时间运行网站的游泳池。提高内存分配和请求限制。

完整的答案

如果你想知道什么时候和为什么东西正在从缓存中删除,则需要使用记录的项目去除<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcallback.aspx\">CacheItemRemovedCallback在插入选项...然后你就可以使用日志的原因<一href=\"http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemremovedreason.aspx\">CacheItemRemovedReason论据。因此,您可以登录的原因为一体的四家上市的原因之一:


  1. 删除的项目从通过删除方法调用或由指定的相同密钥插入方法调用缓存中删除。

  2. 过期的项目是从缓存中删除,因为它已过期。

  3. 未充分利用的该项目从缓存中,因为它的系统中删除以释放内存中删除。

  4. DependencyChanged 该项目从缓存中删除,因为与它相关的缓存依赖改变。

通常情况下,你会发现过期和未充分是事情没有显式删除对缓存的调用,并没有相关性的原因。

您可能会发现,而通过这个有趣的东西的跟踪,你的项目未过期或未被充分利用。相反,我怀疑你会发现,在AppDomain是越来越卸载。

其中一种方法可能发生由于web.config文件(或bin目录,或的.aspx等)的文件得到改变。对于当这种情况发生时看到重新启动应用程序,以了解更多信息 此页。如果发生这种情况,目前正在等待请求倒掉,缓存清空,在AppDomain卸载。您可以通过检查检测到这种情况的<一个href=\"http://msdn.microsoft.com/en-us/library/system.appdomain.isfinalizingforunload.aspx\">AppDomain.IsFinalizingForUnload和记录的回调过程中。

的另一个原因的AppDomain回收是当IIS决定回收程序池为任何的已配置的原因。这样的例子是的 XXX 的内存已经分配过一生,的 YYY 的为程序池运行时的秒的 TTT 的计划的回收时间,或 IIII 的空闲时间(无请求来电)。欲知详情请检查这篇文章IIS6 或的this文章IIS7

I'm trying to cache a price value using HttpRuntime.Cache.Insert(), but only appears to hold the value for a couple hours or something before clearing it out. What am I doing wrong? I want the value to stay in cache for 3 days.

HttpRuntime.Cache.Insert(CacheName, Price, null, DateTime.Now.AddDays(3), TimeSpan.Zero);

解决方案

Short answer

Your application pool or website is being shutdown too soon. Extend the idle timeout on the site, extend the application pool lifetime for the pool running the site. Raise the memory allocation and request limits.

Full answer

If you want to know when and why something is being removed from the cache, you need to log the item removal using the CacheItemRemovedCallback option on the insertion... Then you can log the reason using the CacheItemRemovedReason argument. You can thus log the reason as one of the four listed reasons:

  1. Removed The item is removed from the cache by a Remove method call or by an Insert method call that specified the same key.
  2. Expired The item is removed from the cache because it expired.
  3. Underused The item is removed from the cache because the system removed it to free memory.
  4. DependencyChanged The item is removed from the cache because the cache dependency associated with it changed.

Typically, you will find Expired and Underused being the reasons for things that don't have explict Remove calls made against the cache and don't have dependencies.

You will likely find out, while tracing through this fun stuff, that your items are not being expired or underused. Rather, I suspect you'll find that the AppDomain is getting unloaded.

One way this can happen due to the web.config (or bin directory, or .aspx, etc.) files getting changed. For more information as to when this occurs see the Application Restarts section of this page. When that happens, the currently pending requests are drained, the cache emptied, and the AppDomain unloaded. You can detect this situation by checking the AppDomain.IsFinalizingForUnload and logging that during the callback.

Another reason for the AppDomain to recycle is when IIS decides to recycle the AppPool for any of the reasons it has been configured with. Examples of that are xxx memory has been allocated over the lifetime, yyy seconds of runtime for the AppPool, ttt scheduled recycle time, or iiii idle time (no requests incoming). For further details check this article for IIS6 or this article for IIS7

这篇关于C#HttpRuntime.Cache.Insert()未持有缓存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆