Cache asynchronos refresh

Topics: Caching Application Block
Sep 11, 2009 at 9:23 PM

I've looked all over and can't find any examples for this. It seems to me an obvious case, so either I'm mis-understanding something, or using caching in an illogical manner. You be the judge.

I have a block of data I cache on first use. It is a (relatively) expensive call time wise, say 10 seconds, but is called during user interaction when we want sub second response....hence the cache. The data backing the cache is updated nightly, so I set the cache expiration to 24 hours (AbsoluteTime). So far so good, works as expected.

However, when the expiration hits, the user that made the first call after the expiration is hit with a 10 second delay, and because I lock access, all users that come in during those 10 seconds are delayed until the cache is refreshed. I want to smooth this out by returning the old cache item until the new one is loaded, then the time for the lock will be only long enough to swap one object with another in memory. However, there doesn't seem to be a built in way to do this. When expired the old object is nulled out immediately. I played with the ICacheItemRefreshAction but that isn't proactive, it is only called once the call to the cache is then the user is still waiting 10 seconds.

Isn't this a common problem? Am I missing an obvious solution?


Sep 14, 2009 at 8:43 AM


How can I repro the 10 second delay? Can you provide the steps and code snippets if possible?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Sep 14, 2009 at 1:21 PM

Oh, it could be anything, a long running process, a timer. In my case I'm reading about 47,000 rows of data from a database and doing some massaging of it into some data structures. Reducing the time to run the process though isn't the issue. What I want to do is, on Expiration of a cache object, keep returning the existing cache object to callers of the data, and asynchronosly load a new object. Once the new one is ready it is just a pointer swap in essence (using C# though, so implied pointer swap). I have an idea on how to do this but it doesn't really utilize the Cache Application Block that much. Basically I would hold the data in a static variable, and cache a dummy object. When someone would request the data I'd return it from my static variable and asynchronosly check the dummy object in the cache for expiration and refresh my static variable if necessary. It'll work, but isn't really best practice. And it won't scale if we start using something like Velocity or some other cache framework.

Sep 15, 2009 at 6:28 AM
Edited Sep 15, 2009 at 6:29 AM


Currently we are still investigating the behavior that you want. I'm looking at implementing a ICacheItemExpiration. But I'm not yet sure if it will work out well. I'll get back to you.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Sep 15, 2009 at 3:14 PM

It seems to me an easy way would be to make the ICacheItemRefreshAction optionally event based. Instead of checking for the expiration of an item only on the next read of that item from the cache, allow for the caller to request a refresh event. If the object expiration policy is set to 2 hours, then in 2 hours fire the refresh event, that would allow for asynchronous data refresh. 

Thanks for looking into this, in the meantime I will do my temporary solution of storing the object in a static variable, and caching a dummy object (possibly an integer).

Sep 15, 2009 at 3:18 PM

It would be possible to use two cache items to alleviate this.

Some sort of a cache-of-a-cache solution.  Of course, this leaves you to do the firing of the refresh asynchronously yourself.