Thursday, March 20, 2014

Output Caching with Web API

One of the nice features of Microsoft's MVC template was the ability to just tag a method with the OutputCache tag, specify a few properties and be done with it. This would cache the response (providing a 304 HTTP status code instead of a 200) for a given period of time (among other factors).

Web API is the new RESTful project type that is available in VS 2012 (you actually create an MVC 4 project and have the ability to create controllers that inherit from ApiController instead of the plain Controller). Unfortunately, Web API doesn't have the option to use the OutputCache tag. You can technically put it on a method, but it won't actually do anything. Fortunately, fortune smiles upon us in the form of Filip W.'s CacheOutput package available from Nuget. You can check out Filip's blog post about it here and you can find it in Nuget by searching for Strathweb. I stuck with the 1.0 version as that was more suitable with the project I was working on, but I trust that version 2 would work just as well.

Utilizing the output cache, especially with large result sets, database-intensive queries, and in the RESTful architecture, can greatly improve the performance of your application. Just as an example, we took a response consisting of 37,000+ rows of somewhat complex data in JSON format down from 1500 milliseconds to 10 milliseconds. Yes, 1500 down to 10. That powerful.

No comments:

Post a Comment