Hi
friends, in this article I will explain about the different types of caching in
ASP.NET?
In previous article i explained one interview question ASP.NET Page Life Cycle Events or what is the sequence in which ASP.NET events are processed?
- 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.
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"
%>
|
I will explain the Outputcache example in the next article,How to use OutputCache directive with Substitution in ASP.NET || ASP.NET Page OutputCache Example
2. Fragment
caching:
Sorry for the inconvenience, I will explain the Fragment caching in the next article Fragment Caching in ASP.NET or How To Perform Fragment Caching in ASP.NET by Using VB.NET/C# or Fragment Caching Example, Page Fragment Caching in ASP.NET. What is Fragment Caching in ASP.NET?
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