Most Output caching can be implemented very easily in MVC4 by using the standard OutputCacheAttribute class without having to go down the route of building a custom output cache provider. By decorating your controller actions with the outputcache attributes , you can maintain control over the duration of the cache of your actions.
First off , it's important to note that by default actions in asp.NET MVC have a cache-control of private.Which you can see by creating a simple action like the one below :
public JsonResult Details(int id)
{
////code here to get your results for your id
return Json(result, JsonRequestBehavior.AllowGet);
}
And then checking the header in the response in Fiddler or your tool of choice , and you will see the cache-control :
Cache-Control:private
Connection:Close
Content-Length:81836
Content-Type:application/json; charset=utf-8
Date:Mon, 29 Oct 2012 08:08:44 GMT
Server:ASP.NET Development Server/11.0.0.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
ASP.NET MVC Build-in caching mechanism
You can control output caching by using the build-in output cache in asp.NET MVC. To do this you can decoration your controller actions with the outputcache attribute as follows.
[OutputCache(Duration = 6)]
public ActionResult Search(string searchText)
{
return Json(SearchWebService.MyWebServiceInstance.AutocompleteSearchClient(searchText),JsonRequestBehavior.AllowGet);
}
Now if we check our headers again , we can see our cache-control change :
Cache-Control: public, max-age=6
Expires: Mon, 29 Oct 2012 09:18:37 GMT
Last-Modified: Mon, 29 Oct 2012 09:18:34 GMT
Connection:Close
Content-Length:81836
Content-Type:application/json; charset=utf-8
Date:Mon, 29 Oct 2012 08:08:44 GMT
Server:ASP.NET Development Server/11.0.0.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
To see it in action for yourself , create a simple action like the one above , at first without the caching attribute and put a breakpoint in your controller action , you will note when you F5 the page , that it will hit the breakpoint every time.After decorating your action with the atttribute above , if you then run up your application , and F5 your page again , you can see that after the initial breakpoint being hit , the contents are cached and the breakpoint isnt hit again until your cache duration times out.
VaryByParam
The VaryByParam allows you to have different cached versions of the very same content. This property enables you to create different cached versions of the very same content for example when a query string parameter varies. By setting the VaryByParam property to “none” ,when the action is invoked, the same cached version of the action is returned i.e. any query string parameters are ignored , so usually if we have an action for a particular id , we'd want this to be cached based on the id , so we dont get the same results back.If we have a number of params , and we want them all to be noted in the cache , we can either specific them semi-colon seperated list, or use the * in the VaryByParam value. E.G in the action which populates a grid below , we dont want the paging , sort and id queryparams to be ignored.
[OutputCache(Duration = 1, VaryByParam = "*")]
///code to get all accounts
return PartialView("_AccountsList", data);
}