Caching in ASP.net with Examples..?


In this artical we discuss what is caching, different types of caching in asp.net , how to implement caching in asp.net applications, and how to creat custom controls and how to consuming the dll with examples.

ASP.net Caching:



Caching is used to improve performance of Application like consistency & looking. We can virtually provide results to user and run the Application more faster. Cache means programmed memory Area.

ASP.net supports caching in different concepts but the important areas. Where user has to involve and produce results are 3 types.

1)Page output caching
2)Data Caching
3)Fragment Caching

All these caching methods objective is same but the only diff is what they cache.

1)In page output caching entire processed page request is cache. So , that for every request we provide output to uses from cacheinstead of performing process it.

2)It is also important to understand ASP.net page life cycle with caching & without caching . User has to explicitly add cahcing support in order to cache the page. Different directives are provided to implement page output cache.




1)Page Output Caching:



Design the page with all content regularly and write code according to requirement.

Go to directives section and add
<%@OutPutCache Duration="n sec" VaryByParam="none/*/param"%> to the page.
Here , Duration specifies the amount of time we want to Cache the page OutPut Results.
VaryByParam is used to specify parameter based Caching when not required we specify none.

EX:

1) In a new page drag & drop label, textbox, button controls
2) On button click evetn write below lines of code.

Protected void btn_Click(Object sender,EventArgs e)
{
Label1.Text=DateTime.Now.ToLongtimeString();
}
3) Go to directives section and add
<%@ OutPutCache Duration="60" VaryByParam="none"%>.


If we specify any parameter for VaryByParam attribute then ASP.net store different cache results for different values. But the load to maintain values will also increase. Parameter can be any control or QueryString value.

2)DataCaching:



In Data Caching we will Cache only the Data which we process with ours statement. It can be a simple varriable or it can be a complex type. Cache object of ASP.net is used to create DataCaching this object is provided to overcome the drawbacks of Application object. Like consistency and looking and some programatic issues. Apart from this Cache obj has exclusive advantages like Time Based Expiry, Dependence Base Expiry, Managed By Runtime.

EX:

1)In a new form drag & drop button , link button name as Refresh and GridView controls.
2)On your button click event write the below lines of code.


DataSet ds;
Protected void btn_click (Object sender,EventArgs e)
{
If(Cache["ds"]==null)
{
ds=new DataSet();
ds=//Bind Data;
Cache["ds"]=ds;
}
Else
{
ds=(DataSet)Cache["ds"];
}
Gv.DataSource=ds;
Gv.DataBind();
}

3)On your link button click event write below lines of code.

Protected void lnkbtn_Clck(Object sender,EventArgs e)
{
ds=new DataSet();
ds=//Bind Data;
Cache["ds"]=ds;
Gv.DataSource=ds;
Gv.DataBind();
}



The reason to provide Refresh button to our page is to make or generate consistence output. Without refresh button if we want to manage consistency logically. Then we can create admin page in order to update the Cache Data apart from doing logically we can also use TimeBased or DependencyBased features to provide consistency result.


The timebased Expire in Data Caching:

When we cache data by default it will be available as long as our Application is running apart from logical methods another updations methods that we can useare time based & dependency based expiry.

Time Based Expire:
In time based expire we will cache data and provide time for expire. So that we can reload over data from original datasource.
Cache object has insert method for creating time waste and dependence base expire object.

Ex:

If(Cache["ds"]==null)
{
ds=//Bind Data;
//Time Based Expire
Cache.insert("ds",ds,null,DateTime.Now.AddMinutes(1),TimeSpan.Zero);
}
Else
{
ds=(DataSet)Cache["ds"];
}
Gv.DataSource=ds;
Gv.DataBind();


Note: After 1 min data will be loss. Why because here we add minutes 1 .

Dependency Based Expiry:

To create dependency base expiry we use some insert method but with cache dependence class which will perform dependence based expiry. Some time we can't have logical update to our Cache Data and also time base expiry may not be better solution. In such cases we will go for dependency based Expire.
Note: We do either time based (or) dependency based not both at a time

Cache.Insert("ds",ds,new System.Web.Caching.CacheDependency(MapPath(.xml)));

3)Fragment Caching:



In this method we will Cache some part of our webpage unlike page output caching where entire data is Cache. Fragment caching is implemented with a concept but not with any new command or directive. Fragment caching is very important as we use web user control concept in it.

In ASP.net user defined control vis created in 2 ways
-> Using web User Control
-> Using Custom control.

For any project user defind controls are required and it is very important to create new property and consume it with web user control we can create user defind controls with in the project and consumed it in the same project.

Web User Control:
• Add a new item -> web user control
A file with extension .ascx( Active Server control).
This file is created with< %@Control.....%>
Directive instead of <%@Page....%>
No default content like HTML, head are present in this file.
• Add <%@ OutPut Cache directive to the control.
• Go to Page and register to the control as it is compulsory to consuming a control.

Custom Control:

with custom control also we can create ASP.net user defined control. Most of the standard controls are like custom controls. Unlike web user control. A custom control is prepared as .net library assembly . i.e. .dll. a custom control is created using the base control called web control. Which doesn't provide any functionality for a control. All content should be created and managed by developer only VS.net provides a separate project template for creating a custom control. We can use that and build a .net dll containing ASP.net control.

The following steps are followed fro developing custom control.
• Start file -> new project -> web -> ASP.net server control.
• A .cs file is created where a clas will be extending "web Control" class. Which is base class for all controls. Sample property & a method for displaying the control initial or processed output is also provided.
• User has to add all control functionality here i.e. in a class required for preparing a best control.
• We can add more controls to our project using add new item and ASP.net server control template. Every cotrol is designed with same component building style.
• After preparing all these controls we have to just build our project and the result will be a .dll file containing all these controls.

Consuming the .dll file:

• Start a web site and create a tab in toolbox. So , that controls are not mixed up with other categories.
• Right click on the created tab and select choose items which will result in displaying of window showing all controls components that we can add / remove to our project.
• Browse our .dll file which is created and deserve how they are displayed in toolbox.
• From toolbox we can simply drag & drop asp.net automatically register created a control in pages.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: