ASP.NET Output Cache - Cleaning all at once

Posted on 3:52pm 2/05/2009 by Bruno Silva in .NET, ASP.NET, Programming, Web

I’ve been using a feature of ASP.NET called Output Cache. It allows you to have server-side cache for your ASP.NET webpages. You can define several profiles and cache expiration rules. You can read more about it in a simple and short article at MSDN or in the full documentation also at MSDN.

A feature that I wasn’t able to find was cleaning all the cached data. You can remove cache for a specific page using HttpResponse.RemoveOutputCacheItem(”pagename”) but to remove all pages (which can have several cached versions each) you have to use some kind of workaround. You can use a Cache dependency relying in a item stored in HttpContext.Cache items collection, but for my particular scenario it wasn’t the right solution. Why?

If you have several front-end servers serving your website, you have independent cache systems, so when forcing a cache cleaning, you will force it only for the front-end serving your request. To solve this issue I’ve created a cache system relying on a file dependency. This way, when I change that file, all the front-end servers will clean their cache. I’ve included a CacheManager class that has the usual Add method, that automatically add the file dependency to cached data ( usually using Response.Cache.Add) and not only output cache.

Download source code and have fun exploring it.


One Response to “ASP.NET Output Cache - Cleaning all at once”

  1. Simple Stuff

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Bruno Silva | Powered by Wordpress