Tuesday, December 10, 2013

Caching in ASP.NET MVC : What, Why and How

What is Caching in ASP.NET MVC ?

Caching is process of storing frequently used information and within high speed memory.


Why do we need caching in ASP.NET MVC ?

Most of the times ASP.NET application requirements makes the scenario to get user and page data from the server in all most every request. Because of this kind of accessing data from server each time directly effect the application performance. To avoid this kind performance and latency issues we can use Caching mechanism.

How do we manage caching in ASP.NET MVC ?

Page Output Caching
- In this method browser cache HTTP GET request for predefined time period. When user request same URL again that call not going to server, instead of that it returns already cache page. Following code snippet show the way of doing it.

[OutputCache(Duration=60, VaryByParam="None", Location="ServerAndClient")] Public ActionResult Index() { return View("Index"); }

Duration defined the time period caching should happen. Setting by location says where we wants to do the caching. In this code it done by both of server and client. Because of that user who request this url from another browser also not send the request to the server. We can defined place to locate the cache. VaryByParam create the different version of cache based on the form or query string parameters. You can create cache profile in the web config and use it in with controller.

Donut and Donut Hole Caching
- Donut and Donut hole caching also belongs to the page caching mechanism. But these two methods not cache full page like in output cache. Donut cache is the server side methodology and it cache entire page other than pieces of dynamic content. Donut hole cache do the opposite of it cache selected sections of the page and not cache entire remaining part of the page.

Windows App Fabric Caching Service
- In web farm solution implementing the caching mechanism getting more complex because of each server share the information created in one sever. To solve this kind of scenario use windows app fabric caching service. ASP.NET is the cache client. Cache client can hold the cache in it local store and communicate with cache cluster. When request the data first look in the local store then it look for the cache cluster. If cache cluster not contain that information looking for then it has go to original information source and get it.  

Data Caching 
- In your business application most of the time request same data frequently. This process directly effect to the performance of the application. Data caching is the solution for this kind scenario. Data caching can apply to the layers between data access and business logic. In between these two layers can cache the frequently request data then each time no need to make database request. Data caching use ObjectCache  and MemoryCache objects for the implementation. 

Application Caching 
- In this method use Application Cache API (AppCache) in HTML5. This give access to the local browser cache. To use application cache needs to create application cache manifest, reference manifest and transfer manifest to the client. When application cache enabled browser fetch it only three cases,
a). When makes the any changes to the manifest
b). When clear the cache by user
c). When cache updated by programmatically


No comments :

Post a Comment