Setup ASP.Net caching using a super simple fluent interface
February 10, 2009
While reviewing what I did to Create an RSS Feed Using ASP.Net. We got worried that overzealous customers or feed readers might hit this feed kind of often, and since the data served up by the feed does not change too a lot we could easily do some caching. Luckily this is easy to do using ASP.Net’s built in caching support. Unfortunately the code to, programmatically, setup the caching is a little ugly. To make things easier to read I added a couple extension methods to create a very poor man’s DSL. Following the Microsoft code example is ugly Seeing the code below in my HttpHandler made me cringe. Hopefully it makes you cringe too. TimeSpan freshness = Timespan.FromHours(1); DateTime now = DateTime.Now; context.Response.Cache.SetExpires(now.Add(freshness)); context.Response.Cache.SetMaxAge(freshness); context.Response.Cache.SetCacheability(HttpCacheability.Server); context.Response.Cache.SetValidUntilExpires(true); context.Response.Cache.SetValidUntilExpires(true); context.Response.Cache.VaryByParams["days"] = true; The essence of what I want to do…