InterviewSolution
| 1. |
What is Output caching in MVC |
|
Answer» when we want to improve the performance of the application we use output caching, it prevents the duplicate content to be loaded by caching the content returned by the controller, when the controller methods are invoked, it is the best method to reduce the server round trips, REDUCES database server round trips, reduces network traffic etc. Let’s Suppose when we want to display the list of records from the database in the view , so every time when the user invokes the controller method the query will be executed which will reduce the performance of the application, So, we can advantage of the "Output Caching" that avoids executing database queries each time the user invokes the controller method. It is not necessary that the DATA will be retrieved until the time we have given, When memory resources become low, the cache starts evicting content automatically, and thus increase the performance of the Project. The DURATION is in seconds by default the value used is 60 seconds [HttpPost] [OutputCache(Duration = 10, VaryByParam = "name")] public ActionResult SearchCustomer(string name = "") { NorthwindEntities db = new NorthwindEntities(); var model = from r in db.Customers where r.ContactName.Contains(name) select r; if (model.Count() > 0) { return View(model); } ELSE { return View(); } }In the above code the "name" parameter passed by the user and then, depending on the name, selecting matching records with a LINQ query and then checking if the model has the number of records greater than zero then send the model to the view else simply send the view (no model),VaryByParam="name" and "VeryByParam" is something that makes many differences 1.VaryByParam = "none": Think of it like, we don't want to care about the form parameter or query string parameter passed by the user from the view PAGE. If I use "none" then it will create the same cached version of the content for every user who visits the website, 2.VaryByParam = "name": By using this we can create different cached versions of the content when the query string parameter is varied from one param to other. In other words if I find records matching "ce" string then a new cache will be created by replacing the older one, again if I find records matching "ab" string then a new cache will be created by replacing the last one ("ce" cached), no matter duration is elapsed or not. |
|