Scott on Writing

Musings on technical writing...

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:

  1. 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.
  2. 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.

posted on Sunday, April 10, 2005 10:37 PM

Feedback

# re: Programmatically Removing a Page from the OutputCache 4/11/2005 10:22 AM Ryan Trudelle-Schwarz

One thing I've trained myself to do when reflecting on anything, especially if I end up not seeing what I need, is to browse through the callee lists. For instance, in this case I went through these steps and found the method you note above:

1) Look at the source for OutputCacheModel.Init().
2) Seeing that it is interacting with the CacheInternal object, browse that.
3) Since it is private, and there is a method called "DoRemove", I browsed to see who calls that.
4) That method is called by 4 other methods, one of which is public (Cache.Remove()).
5) Looking at Cache.Remove, it will only work for public entries so not in this case.
6) Back in the callee graph for CacheInternal.DoRemove, there is only one public method that calls any of the other 3 methods which call DoRemove, HttpResponse.RemoveOutputCacheItem.

This case was quicker since I knew what I wanted to find, but generlaly it doesn't take all that long.

# re: Programmatically Removing a Page from the OutputCache 4/11/2005 1:48 PM Steve Ewart

Scott,

What would be an example of when you would want to delete a page from the cache, wouldn't IIS detect a change to a page and realize that the page should be re-cached?

Thanks,
Steve

# re: Programmatically Removing a Page from the OutputCache 4/11/2005 8:56 PM Scott Mitchell

Steve, if you changed the content of the markup portion of the ASP.NET page or recompiled the assembly then, yes, ASP.NET would realize there's a change and update it. But suppose that you were showing database data in an output cached page and the database data changed. This wouldn't cause the page's output cache to be invalidated. However, you might want to be able to programmatically destroy the cache, and touching the physical file that corresponds to the page might not be an option... in this situation it would be nice to be able to invalidate the output cache through some web-based interface.

# re: Caching Talk in Columbus 4/30/2005 1:41 PM Steven Smith

# Removing a version of a page 5/12/2005 7:44 AM Ben Mills

I hope that the output cache APIs are improved in Whidbey. One big weakness of the RemoveOutputCacheItem method is that it removes all versions of the cached page. In my case I have a Product.aspx page which takes a ProductID parameter. I specify VaryByParam="ProductID" so that a different version of the page is cached for each ProductID. When a product is updated in the admin part of my site I want to be able to remove the cached version of Product.aspx for that product only. Unfortunately, RemoveOutputCacheItem will remove all cached versions of the page for all products.

# re: Programmatically Removing a Page from the OutputCache 7/8/2005 4:26 AM JLK

Try HttpResponse.RemoveOutputCacheItem(Request.CurrentExecutionFilePath)

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles