Wednesday 14 August 2013

Different Types of Caching in ASP.NET | ASP.NET Page Output Cache | Improving Performance with Output Caching

 Hi friends, in this article I will explain about the different types of caching in ASP.NET?
  •  Caching is a powerful feature in ASP.NET that can increase the performance of a Web application. Caching is generally used to catch frequently accessed data. 
  • Basically we are retrieving data from a database like SQL Server, Oracle etc. is one of the slowest Web site operations that we can perform. 
  • However, if you can cache the database data in memory and avoid accessing the database with every page request, you can dramatically increase the performance of your application.
Benefits:
Page rendering is faster.
Decrease database hits.
Decrease the consumption of server resources.
Types:
There are three types of caching in ASP.NET.
1. Page Output caching
2. Fragment caching
3. Data Caching
1. Page Output caching:
  • It simply keeps a copy of the response that was sent to the client in memory and subsequent requests are then responded with the cached output until the cache expires, which incredibly improves the ASP.NET web application performance.
  • You cannot add an OutputCache directive on a master page. You must do the caching instructions in code. Every content page that uses the master page needs output caching enabled.
In VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(5))
        Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate)
        Response.Cache.SetValidUntilExpires(True)
    End Sub
In C#:
Protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMonths(1));    Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
    Response.Cache.SetValidUntilExpires(true);
}
The below figure shows how it works

In ASP.NET Output Cache, ASP.NET uses the directive @ OutputCache. We can declare the following attributes to control the output caching of the ASP.NET or a user control in an ASP.NET page.

<%@ OutputCache Duration="Numberofseconds"
   Location="Any|Client|Downstream|Server|None ServerAndClient"
   VaryByParam="parametername"
   Shared="True|False"
   VaryByControl="controlname"
   VaryByCustom="browser|customstring"
   VaryByHeader="headers"
   VaryByContentEncoding="encodings"
   CacheProfile="cache profile name|''"
   NoStore="true|false"
   SqlDependency="database/table name pair|CommandNotification"
%>

2. Fragment caching:
3.Data Caching:
Sorry for the inconvenience, I will explain the Data caching  in the next article.
I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.