Design the application architecture - Objective 1.5: Design a caching strategy Flashcards
What is caching?
A strategy to improve performance by storing frequently used information in high-speed memory.
How do you implement page output caching in ASP.Net MVC?
Add the OutputCache attribute to your action method.
Ex:
[OutputCache(Duration=120, VaryByParam="Name", Location="ServerAndClient")] public ActionResult Index() { return View("Index", myData); }
Which property of the OutputCache attribute is used to configure the length of time to cache an action’s result?
Duration
Ex:
[OutputCache(Duration=120, VaryByParam="Name", Location="ServerAndClient")] public ActionResult Index() { return View("Index", myData); }
How do you use the OutputCache attribute to disable caching?
Set Duration to zero.
Ex:
[OutputCache(Duration=0)] public ActionResult Index() { return View("Index", myData); }
What are the valid values for the Location property of the OutputCache attribute?
Any, Client, Downstream, None, Server, ServerAndClient
What is the VaryByParam property of the OutputCache attribute used for?
Specifies the query string or form parameter names to store different versions of the cached output.
In the example code below, the output will be cached separately for each distinct value passed for Name in the query string of the request.
Ex:
[OutputCache(Duration=120, VaryByParam="Name", Location="ServerAndClient")] public ActionResult Index() { return View("Index", myData); }
What is donut caching?
Allows caching of an entire page except for the dynamic parts of the page.
How is donut caching achieved in ASP.Net MVC?
Partial views instead of the entire page are cached.
What is the ChildActionOnly attribute used for?
Only allows the action to be called from Action or RenderAction HTML extension methods.
Ex:
[ChildActionOnly] [OutputCache(Duration=60)] public ActionResult ProductsChildAction() { // Fetch products from the database and // pass it to the child view via its ViewBag ViewBag.Products = Model.GetProducts(); return View(); }