Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing frequency of ASP.NET cache item expiration?

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.

like image 388
Jan Zich Avatar asked Nov 06 '08 12:11

Jan Zich


1 Answers

Poking around with Reflector reveals that the the interval is hardcoded. Expiry is handled by an internal CacheExpires class, whose static constructor contains

_tsPerBucket = new TimeSpan(0, 0, 20);

_tsPerBucket is readonly, 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()...

DateTime utcNow = DateTime.UtcNow;
TimeSpan span = _tsPerBucket - new TimeSpan(utcNow.Ticks % _tsPerBucket.Ticks);
this._timer = new Timer(new TimerCallback(this.TimerCallback), null,
    span.Ticks / 0x2710L, _tsPerBucket.Ticks / 0x2710L);

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 is internal, 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 returns null. 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.

like image 70
stevemegson Avatar answered Sep 29 '22 15:09

stevemegson