Caching in MVC5 ?

Caching in MVC5

Caching is the most important aspect of high-performance web application.Caching is way of storing data when fetching of data is frequent hence praticlly imprving performnce of application.

Advantages of Caching
  • Reducing round trips
  • Reducing Server database round trips
  • Better Performance
  • Lesser Network Traffic
For Caching in MVC we use Output Cache Filter.

Output Cache Filter
This filter will allow us to cache the data for any action method for the specified time, by default the time limit is 60 seconds. This means when any cal is made the data will be cached for next 60 minute after that 60 seconds if there is any call the data will be cached again.
Example of using Output Cache Filter


[OutputCache(Duration=20, VaryByName="none")]
public ActionResult Index()
{
ViewBag.Message=DateTime.Now.ToString();
return View();
}


Here the code will cacche the index page for next 20 seconds. We can check the cache effect by placing the ViewBag.Message on the index view page.The result will remian the same for the next 20 seconds.

Output Cache Location
This is vary usable depending upon the usage. Now with caching there will be problem if we want to cache the personal data of the user, If the data is cached on the server the details remain the same for other users also. Thus there is location parameter in the Output Cache Filter.

You can set the Location property to any one of the following values:
  • Any
  • Client
  • Downstream
  • Server
  • None
  • ServerAndClient

Comments

Popular posts from this blog

Inserting Data using EF with MVC4.

Power Apps Choice Column Default Value with connector as Sharepoint