I noticed that the ASP.NET cache items are inspected (and possibly removed) every 20 seconds (and oddly enough each time at HH:MM:00, HH:MM:20 and HH:MM:40). I spent about 15 minutes looking how to change this parameter without any success. I also tried to set the following in web.config, but it did not help:
<cache privateBytesPollTime="00:00:05" />
I’m not trying to do anything crazy, but it would be nice if it was, say, 5 seconds instead of 20, or at least 10 for my application.
Best Solution
Poking around with Reflector reveals that the the interval is hardcoded. Expiry is handled by an internal
CacheExpires
class, whose static constructor contains_tsPerBucket
isreadonly
, so there can't be any configuration setting that modifies it later.The timer that will trigger the check for expired items is then set up in
CacheExpires.EnableExpirationTimer()
...The calculation of
span
ensures that the timer fires exactly on :00, :20, :40 seconds, though I can't see any reason to bother. The method that the timer calls isinternal
, so I don't think there's any way to set up your own timer to call it more often (ignoring reflection).However, the good news is that you shouldn't really have any reason to care about the interval.
Cache.Get()
checks that the item hasn't expired, and if it has then it removes the item from the cache immediately and returnsnull
. Therefore you'll never get an expired item from the cache, even though expired items may stay in the cache for up to 20 seconds.