Blog Post

How to handle Output Caching in the development environment for ASP.NET MVC applications

Ideas & Innovation
How to handle Output Caching in the development environment for ASP.NET MVC applications
  • 23
    Jul
  • Image

How to handle Output Caching in the development environment for ASP.NET MVC applications


Caching is a wonderful thing! Many applications benefit from the caching of data or web pages with increased performance, scale & user experience (UX). How often is it though that something great turns out to increase development effort due to some side effect.

I found myself using the OutputCache attribute on my controller actions (AOP). The code was clean and the application functioned wonderfully. Then low and behold none of my breakpoints were being hit when debugging - of course the culprit was that my (dynamic) pages/views were being loaded from the browser & server cache. How could I prevent this without removing the OutputCache attribute?

I tried using something like this:

#IF !DEBUG
[OuputCache(Duration = 300, VaryByParam = "*", Location = OutputCacheLocation.ServerAndClient)]
#ENDIF
public ActionResult Index() {
…
}

However that is very messy and I simply was not pleased with this hack. I, as you do, googled but found no ideal solution so I thought - why don't I just extend the OutputCache attribute and provide my own logic of when to cache and not cache. It's key to extend the OutputCache attribute as I didn't want to rewrite the functionality already provided, but simply apply some further rules.

public class CustomOutputCacheAttribute : OutputCacheAttribute
{
	public override void OnActionExecuted(ActionExecutedContext filterContext)
	{
		HttpRequestBase request = filterContext.HttpContext.Request;

		if (!request.IsLocal)
		{
			base.OnActionExecuted(filterContext);
		}
	}

	public override void OnActionExecuting(ActionExecutingContext filterContext)
	{
		HttpRequestBase request = filterContext.HttpContext.Request;

		if (!request.IsLocal)
		{
			base.OnActionExecuting(filterContext);
		}
	}

	public override void OnResultExecuting(ResultExecutingContext filterContext)
	{
		HttpRequestBase request = filterContext.HttpContext.Request;

		if (!request.IsLocal)
		{
			base.OnResultExecuting(filterContext);
		}
	}

	public override void OnResultExecuted(ResultExecutedContext filterContext)
	{
		HttpRequestBase request = filterContext.HttpContext.Request;

		if (!request.IsLocal)
		{
			base.OnResultExecuted(filterContext);
		}
	}
}

Simply use in the same way as you would with the standard OutputCache attribute.

[CustomOuputCache(Duration = 300, VaryByParam = "*", Location = OutputCacheLocation.ServerAndClient)]
public ActionResult Index() {
…
}

While I have only checked for whether the request is local, a whole raft of rules can be applied. Hope this article helps and feel free to leave your comments.

Comments (2)

Darius Harris 21 March 2013, 10:31 AM Website

Yes its an essential think but fist should know about that we are familiar with framework or not because its very important.

vincent 08 September 2013, 02:38 PM

It seems misused AOP. Why not use CacheProfile and web.config transformation to handle the different environment cache settings?

New Comment

Notify me of follow up posts