ASP.NET output caching - dynamically update dependencies
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.
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.
You can manually invalidate a cached page using:
System.Web.HttpResponse.RemoveOutputCacheItem(path)
精彩评论