1
votes

I'm trying to prevent output cached page from being ejected from my asp.net output cache unless another one can replace it. In other words, if a page has expired (based on Duration property of OutputCache profile) but accessing the page now results in an exception, I just want to serve the old, outdated page. I was hoping the custom cache providers in ASP.NET 4 would help my situation but they don't get far enough up the pipeline.

Here's what I know:

  • System.Web.Caching.OutputCache is really hard to read through
  • A custom cache provider hooks in after all dependencies and is fed a CachedVary object.
  • The utcExpiry date passed into the custom cache provider's Add and Set methods is just DateTime.Max
  • The actual expiration of the cache item happens higher up in the pipeline
  • Even for expired items, the custom cache provider's Get method is still called. It returns the cached CachedVary object and then my controller's action method is called. After this, the Add method is called with the CachedVary object and Set method is passed an actual cached page data object.

Can someone with more experience hacking outputcache steer my in a direction here? Can I accomplish what I'm looking for with ASP.NET outputcache? Must I roll my own?

1
It sounds to me like this is almost more of an exception-handling issue. You should not want your exceptions to be completely hidden like this. Catch the exception and regenerate the "old" data somehow... or consider if an Exception is the proper way to handle what is happening in the first place. - Andrew Barber
I actually got a decent pattern worked out here. I'll be writing a blog post at some point and will come back here to post the link in case anyone is interested. - hackerhasid
Andrew -- why not? In my case we're displaying data on a page. If there's an exception generating the data then I like the idea of hiding the exception completely (from the end user, of course devs are alerted!) and displaying old data. We can add a banner to the top explaining that the data was old, etc. - hackerhasid
Then you are not hiding the exception from the end user, and you are not just displaying the old data - you are informing them something went wrong, and showing an old result in the interim. Do you expect these excpetions to be so frequent to affect your caching strategy this much? - Andrew Barber
I expect exceptions to occur from time to time which is frequent enough for clients to get very upset (the moment they see an error page). This helps keep our clients happy :) - hackerhasid

1 Answers