Programmatically Removing a Page from the OutputCache
One of ASP.NET's major improvements over classic ASP is its powerful caching API, which, if rightly utilized, can lead to dramatic performance gains in a Web application. For example, in ASP.NET Micro-Caching: Benefits of a One-Second Cache, author Steve Smith talks about the advantages of using output caching for one-second intervals. If you've ever heard Rob Howard speak you'll no doubt have heard his points on the benefits of caching.
The ASP.NET data cache provides a number of methods to add, inspect, and remove items from the cache, allowing one to build a “caching administration page,” where a site admin can, in real-time, inspect the items in the Web application's caching and opt to clear certain cached items. In fact, if you are going to be in Southern California in early May you can listen to Scott Cate talk about such an application:
What’s in your Cache? - Scott Cate
Do you use the cache for ASP.NET ? If so, do you know what’s in it? How do you remove an object from cache? Sure you can do it programmatically, but what if you had an admin interface to view your items in the cache, and remove them manually. Sure, touching the web.config clears the cache, but it’s an expensive operation that has much more overhead then just “clearing out the cache”. In this session I’ll show you and give you the code that I’ve written to build a cache viewer control panel, to take charge of the cache in your application.
While the data cache provides a nice API to interact with the items in the cache, many developers have wondered if the output cache provides a similar interface. Specifically, I've seen many questions regarding how to programmatically remove a page from the output cache. This is a question that I first stumbled upon perhaps a year ago. In attempting to investigate this problem I turned immediately to Reflector, examining the OutputCacheModule HTTP Module. Examining the source you'll find that the output cache contents are stored to a provide member variable, thereby preventing examination or removal from a page developer. Ick.
So, for the past year or so, I assumed that it was impossible to remove an item from the output cache. And then, tonight, I came across another person asking this same question and (finally) did what I should have done when first exploring this question: I Googled it. One of the first 10 hits was a page from msdn.microsoft.com - Caching Page Output with Cache Key Dependencies. In this document I read the following:
You can explicitly remove any page from the output cache by calling the HttpResponse.RemoveOutputCacheItem method. You can do this from the global.asax file, a custom ASP.NET server control you have created, or from a page, depending upon the needs of your application.
Egad, so it is possible to programmatically evict the cached output of an ASP.NET page. In fact, as the title of the article implies, it is possible to base the output cache of a particular page on a dependency in the data cache, rather than relying just on the specified duration. Color me ignorant.
This experience has taught me two things:
While Reflector is oftentimes the de facto way for deducing how to bowels of the .NET Framework operate, relying on Reflector alone is a recipe for misinformation. Reflector acts as a sort of magnifying glass, allowing intense scrutiny at a specific point in the framework, but the framework is so unweildy and expansive that focusing so intently on a single aspect can make one miss the forest for the trees.
The absolutely, positively, without question first thing one should do when searching for a solution for a problem is to use Google. And that should probably be the second thing done as well. As my last blog post
revealed, chances are someone else has already experiences the exact same problem you have, so utilize their work/time rather than wasting your own.