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)

# re: Programmatically Removing a Page from the OutputCache 10/20/2008 11:51 PM Bute

The HttpResponse.RemoveOutputCacheItem(Request.CurrentExecutionFilePath) still removes all the cached versions of a page.

# How to : Dynamically Remove ASP.NET Page Output Cache Entries 3/9/2009 1:50 PM zc0000

I have sought for a perfect solution for a long time and eventually get following old links : Remove

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

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55618.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3020100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1193.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3020100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.00160
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403020

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.67322
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403020

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles