views:

251

answers:

2

Hi All,

I have an ASP.NET application which requires output caching. I need to invalidate the cached items when the data returned from a web service changes, so a simple duration is not good enough.

I have been doing a bit of reading about cache dependencies and think I have the right idea. It looks like I will need to create a cache dependency to my web service.

To associate the page output with this dependency I think I should use the following method:

Response.AddCacheItemDependency(cacheKey);

The thing I am struggling with is what I should add to the cache?

The dependency my page has is to a single value returned by the web service. My current thinking is that I should create a Custom Cache Dependency via subclassing CacheDependency, and store the current value in the cache. I can then use Response.AddCacheItemDependency to form the dependency.

I can then periodically check the value and for a NotifyDependencyChange in order to invalidate my cached HTTP response.

The problem is, I need to ensure that the cache is flushed immediately, so a periodic check is not good enough. How can I ensure that my dependant object in the cache which represents the value returned by the web service is re-evaluated before the HTTP response is fetched from the cache?

Regards, Colin E.

A: 

I believe you are on the right track with your cache dependency. However, if you don't "periodically check" the return value of the web service, how can you know when it returns a new value? You may need to set up a web service in the other direction, so that when the value changes in the other system, it can call your system and invalidate the old cache and stick in the new value.

Ray
Hi Ray, thanks for your response. The web-service call is inexpensive and is something I can do each time a page is requested. What I need to know is where in my ASP application I should make this check, it should occur before the server determines whether to return a cached response.
ColinE
Well, you could do it on every page request, but that seems wasteful if you have a busy site. If the call is that inexpensive, could you just live with a very short period (a few seconds) between cache tests? Or, is it absolutely essential that the data not be so much as one second out of date? (Of course, the data will be out of date at some point as it sits on the users screen waiting to be read.)
Ray
A: 

You can manually invalidate a cached page using:

System.Web.HttpResponse.RemoveOutputCacheItem(path)
zaph0d
Hi, Thanks for the info - I was not aware of this. When the user requests a page, do I have an oppurtunity to remove the page from the cache before the server checks for a cached response? This would allow me to check the remote web service before each page request is handled.
ColinE
No, you would have to invalidate the cache before the page is requested. Normally, you would do this when the data is updated - either automatically using cache dependencies or manually using the code above.
zaph0d