什么时候Asp.Net删除过期缓存项? [英] When Does Asp.Net Remove Expired Cache Items?

查看:118
本文介绍了什么时候Asp.Net删除过期缓存项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您添加到的System.Web.Caching.Cache 的项目具有绝对的截止日期,如下面的例子,如何Asp.Net表现?做的:

When you add an item to the System.Web.Caching.Cache with an absolute expiration date, as in the following example, how does Asp.Net behave? Does it:


  1. 简单标记为过期的项目,然后执行 CacheItemRemovedCallback 在下次访问尝试?

从缓存中删除的项目,并执行 CacheItemRemovedCallback 立即?

Remove the item from the cache and execute the CacheItemRemovedCallback immediately?

HttpRuntime.Cache.Insert(key,
                         new object(),
                         null, 
                         DateTime.Now.AddSeconds(seconds), 
                         Cache.NoSlidingExpiration,
                         CacheItemPriority.NotRemovable, 
                         OnCacheRemove);


MSDN似乎表明它立即发生。例如,的ASP.NET缓存概述<的过期部分/ A>说:ASP.NET会自动从缓存中,当他们到期删除的项目。同样,从主题的例子如何:当一个项目从缓存中删除通知应用程序说:如果超过15秒调用之间经过,以 GetReport [在本例中的方法],ASP.NET从缓存中删除的报告。

MSDN appears to indicate that it happens immediately. For example, the "Expiration" section of the "ASP.NET Caching Overview" says "ASP.NET automatically removes items from the cache when they expire." Similarly, the example from the topic "How to: Notify an Application When an Item Is Removed from the Cache" says "If more than 15 seconds elapses between calls to GetReport [a method in the example], ASP.NET removes the report from the cache."

不过,这些都不是毫不含糊的。他们不说回调立即执行,我能想象自己怎么作家可能认为选择1以上计为删除的项目。所以,我做了一个快速和肮脏的测试,瞧,这似乎是立即执行 - 我得到定期的,即使没有人访问我的网站第六十二回调

Still, neither of these is unambiguous. They don't say "the callback is executed immediately" and I could conceive of how their writers might have thought option 1 above counts as 'removing' an item. So I did a quick and dirty test, and lo, it appears to be executing immediately - I get regular sixty-second callbacks even when no one is accessing my site.

不过,我的测试是快速和肮脏的,并在评论我的回答<一个href=\"http://stackoverflow.com/questions/1395358/is-there-a-way-to-run-a-process-every-day-in-a-net-web-application-without-writi/1395447#1395447\">Is有没有办法每天都在.NET Web应用程序运行的过程,而无需编写Windows服务或SQL服务器作业中,有人提出,Asp.Net实际上推迟回调的拆除和执行,直到一些尝试访问再次缓存。

Nonetheless, my test was quick and dirty, and in the comments to my answer to Is there a way to run a process every day in a .Net web application without writing a windows service or SQL server jobs, someone has suggested that Asp.Net actually defers removal and execution of the callback until something tries to access the cache again.

任何人都可以权威地解决这个或者这只是考虑一个实现细节?

Can anyone settle this authoritatively or is this just considered an implementation detail?

推荐答案

华友世纪反射

过期的缓存项的真正的删除(称为回调)当任:

Expired cache items are actually removed (and callbacks called) when either:

1)东西尝试访问缓存项。

1) Something tries to access the cache item.

2) ExpiresBucket.FlushExpiredItems 方法运行,并得到项目。这种方法是硬codeD执行每20秒(接受的答案的问题计算器<一个href=\"http://stackoverflow.com/questions/268468/changing-frequency-of-asp-net-cache-item-expiration\">Changing 证实我通过反射这个code)的读取ASP.NET缓存项过期的频率。然而,这需要额外的资格(为其读)。

2) The ExpiresBucket.FlushExpiredItems method runs and gets to item. This method is hard-coded to execute every 20 seconds (the accepted answer to the StackOverflow question Changing frequency of ASP.NET cache item expiration corroborates my read of this code via Reflector). However, this has needs additional qualification (for which read on).


Asp.Net维护一个缓存服务器上的每个CPU(我不知道这是否这些重新present逻辑或物理CPU);每一种主张,都有一个对应的定时调用它的 FlushExpiredItems CacheExpires 实例code>方法,每二十秒钟。

Asp.Net maintains one cache for each CPU on the server (I'm not sure if it these represent logical or physical CPUs); each of these maintains a CacheExpires instance that has a corresponding Timer that calls its FlushExpiredItems method every twenty seconds.

此方法在缓存过期数据的水桶的另一个集合( ExpiresBucket 实例的数组)连续迭代,要求每个桶的 FlushExpiredItems 方法反过来。

This method iterates over another collection of 'buckets' of cache expiration data (an array of ExpiresBucket instances) serially, calling each bucket's FlushExpiredItems method in turn.

这个方法的( ExpiresBucket.FlushExpiredItems )第一迭代桶里的所有缓存项目,如果项目已过期,标志着它已过期。然后(我严重简化这里)进行迭代它标志着过期并删除它们的项目,执行 CacheItemRemovedCallback (实际上,它调用 CacheSingle。删除,要求 CacheInternal.DoRemove ,那么 CacheSingle.UpdateCache ,那么 CacheEntry.Close ,这实际上调用回调)。

This method (ExpiresBucket.FlushExpiredItems) first iterates all the cache items in the bucket and if an item is expired, marks it expired. Then (I'm grossly simplifying here) it iterates the items it has marked expired and removes them, executing the CacheItemRemovedCallback (actually, it calls CacheSingle.Remove, which calls CacheInternal.DoRemove, then CacheSingle.UpdateCache, then CacheEntry.Close, which actually calls the callback).

所有这些连续发生的,因此有一个机会,事情就可以阻止整个过程和存储的东西了(并从其指定的到期时间推缓存项的到期回)。

All of that happens serially, so there's a chance something could block the entire process and hold things up (and push the cache item's expiration back from its specified expiration time).

然而,在这个时间分辨率,为20秒的最小到期间隔,能够阻断的时间显著长度的过程的唯一部分是 CacheItemRemovedCallbacks 。其中任何一项可以想见阻止给定的的定时器 FlushExpiredItems 螺纹下去。 (虽然20秒后,定时会催生另一种 FlushExpiredItems 线程。)

However, at this temporal resolution, with a minimum expiration interval of twenty seconds, the only part of the process that could block for a significant length of time is the execution of the CacheItemRemovedCallbacks. Any one of these could conceivably block a given Timer's FlushExpiredItems thread indefinitely. (Though twenty seconds later, the Timer would spawn another FlushExpiredItems thread.)

要总结,Asp.Net没有的保证的,它会在指定时间执行的回调,但它会在某些情况下这样做。只要到期时间间隔更到20秒之余,只要缓存没有执行耗时 CacheItemRemovedCallbacks (全局 - 任何回调可能干扰与任何其他人),它可以在时间表执行期满回调。这将是足够好的对于某些应用程序,但短期下跌他人。

To summarize, Asp.Net does not guarantee that it will execute callbacks at the specified time, but it will do so under some conditions. As long as the expiration intervals are more than twenty seconds apart, and as long as the cache doesn't have to execute time-consuming CacheItemRemovedCallbacks (globally - any callbacks could potentially interfere with any others), it can execute expiration callbacks on schedule. That will be good enough for some applications, but fall short for others.

这篇关于什么时候Asp.Net删除过期缓存项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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