Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching in the ASP.NET MVC Framework

I am fairly new at using the ASP.NET MVC framework and was hoping that I could find some help about best-practises when caching various parts of my MVC web application. I know that stack overflow uses MVC and some pretty freeking awesome caching techniques, and its MILES faster than my app, even when running locally.

I have a few questions.

  1. How does the caching actually work, and how do you "enable" it, and what are the various options. What is the best kind of caching to use?

  2. My app has a lot of database transactions, lists that regularly change. I am worried about the timliness of page refreshes, in users not getting the most recent version of the data. Whats the best way to strike a balance between web application speed and displaying "up-to-date" data? What best practises have you guys found when having to deal with this issue?

  3. How do I cache different parts (I assume views) with different caching settings? I assume that it could be done with sub-controllers, but I have NFI how to go about doing this.

  4. I am using the Castle.Windor integration to the controllers, I am not sure if this changes anything.

  5. Any other best practises of notes of things to be wary/careful of would be greatly appreciated.

like image 682
Ash Avatar asked Feb 09 '09 01:02

Ash


People also ask

What are the different caching techniques available in .NET MVC?

Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser. Server- Content is cached on the Web Server. ServerAndClient- Content is cached on the Web Server and the Web Browser.

What is caching in ASP.NET Core?

Caching makes a copy of data that can be returned much faster than from the source. Apps should be written and tested to never depend on cached data. ASP.NET Core supports several different caches. The simplest cache is based on the IMemoryCache. IMemoryCache represents a cache stored in the memory of the web server.

How can store data in cache in ASP.NET MVC?

Store data into Cache in ASP.NET MVC in ASP.NET MVC Above action method first checks for the null value in HttpContext. Cache[“MyDate”] and it its null then saves current date in the Cache. Next line simply keep the data from the Cache into ViewBag. DateTime.


2 Answers

You might want to take a look at Phil Haack post for some donut caching. He's THE reference for ASP.NET MVC :)

like image 57
Maxime Rouiller Avatar answered Sep 30 '22 02:09

Maxime Rouiller


From a "best practices" perspective, you will need to consider the same things you must consider in any application that uses caching.

  • Is the traffic volume high enough to benefit from caching?
  • How often does a particular piece of data change? How crucial is timeliness?
  • Do I own the data-access layer? If so, can I trigger the refresh in the cache by the actual changing of the data and avoid a time-based expiration?

and the list goes on.

like image 35
oglester Avatar answered Sep 30 '22 03:09

oglester