blog

Home / DeveloperSection / Blogs / Caching in ASP.NET

Caching in ASP.NET

priyanka kushwaha2352 23-Feb-2015

In this blog I will talk about caching in ASP.NET

Caching is very  important concept for any web application, which is used to store the data temporarily either on web server. This data can be stored for a specific time period on the server or client system. It is more useful when any use access the   same set of data continuously.

Engine(Google, Yahoo etc.) for searching relevant information. If  you open Google page on your system first time then first time Google pages comes from Google database. If  you open again Google search engine then this Google page comes from Google server cache.

Accessing a database in an Asp.net applications generally a slow and time consuming process. To improve the performance of a asp.net page, we can use Caching concept, which is quicker than retrieving the data from the database on each request.

Different types of caching:

ASP.NET providing three basic types of caching:

1.       Page level output caching

2.       Fragment  caching

3.       Data caching

 

Page level output caching:

In this Caching technique page output and HTTP response stored in a memory cache (RAM) of the computer. You can create more than one custom Output Cache provider.

System.web.caching.OutputCacheprovider class is used to create custom output cache provider which can be registered within the web.config file. This caching concept is more useful for static web page. In this  caching data can be stored on  different-2 location such as client side, server side or proxy side.

There are some techniques to implement the page output caching is asp.net application.

1.       Page Output caching Depends on some control values.

2.       Page Output caching Depends on some query string key value.

Note:

1.       This page is  saved  in cache memory for 40  seconds.

2.       You can use XML file instead of  sql server database for this caching  applications. 

2. Fragment Caching(partial page caching):

When we want to implement caching on specific part of a web page then we can use Fragment caching or partial caching. This type of caching is implemented by using webusercontrol.

Example:

<%@PageLanguage="C#"Trace="true"AutoEventWireup="true"CodeBehind="OutputCacheExample.aspx.cs"Inherits="DataCachingExample.OutputCacheExample"%>

 

<!DOCTYPEhtml>

 

<%@OutputCacheDuration="60"VaryByParam="state"%>

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

    <title></title>

 

</head>

<body>

    <formid="form1"runat="server">

        <i>Last generate on:</i>

        <asp:LabelID="TimeMsg"runat="server"Text=""></asp:Label>

          <h3>

            <fontface="Verdana">Using the Output Cache</font></h3>

        <b>Authors by State:</b>

        <tablecellspacing="0"cellpadding="3"rules="all"style="background-color: #AAAADD;

            border-color: black; border-color: black; width: 700px; border-collapse: collapse;">

            <tr>

                <td>

                    <ahref="OutputCacheExample.aspx?state=CA">CA</a></td>

                <td>

                    <ahref="OutputCacheExample.aspx?state=IN">IN</a></td>

                <td>

                    <ahref="OutputCacheExample.aspx?state=KS">KS</a></td>

                <td>

                    <ahref="OutputCacheExample.aspx?state=MD">MD</a></td>

                <td>

                    <ahref="OutputCacheExample.aspx?state=MI">MI</a></td>

                <td>

                    <ahref="OutputCacheExample.aspx?state=OR">OR</a></td>

                <td>

                    <ahref="OutputCacheExample.aspx?state=M.P.">M.P.</a></td>

                <td>

                    <ahref="OutputCacheExample.aspx?state=U.P.">U.P.</a></td>

            </tr>

        </table>

        <asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:MindStickTaskConnectionString %>"SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [DateOfBirth], [FatherName], [AddressLineFirst], [AddressLineSecond], [City], [State], [Country], [PinCode], [PhoneNo], [EmailAddress] FROM [EmployeeDetail] where State=@state">

            <SelectParameters>

                <asp:QueryStringParameterName="state"QueryStringField="state"DefaultValue="CA"/>

            </SelectParameters>

        </asp:SqlDataSource>

        <asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="EmployeeID"DataSourceID="SqlDataSource1">

            <Columns>

                <asp:BoundFieldDataField="EmployeeID"HeaderText="EmployeeID"SortExpression="EmployeeID"InsertVisible="False"ReadOnly="True"/>

                <asp:BoundFieldDataField="FirstName"HeaderText="FirstName"SortExpression="FirstName"/>

                <asp:BoundFieldDataField="LastName"HeaderText="LastName"SortExpression="LastName"/>

                <asp:BoundFieldDataField="DateOfBirth"HeaderText="DateOfBirth"SortExpression="DateOfBirth"/>

                <asp:BoundFieldDataField="FatherName"HeaderText="FatherName"SortExpression="FatherName"/>

                <asp:BoundFieldDataField="AddressLineFirst"HeaderText="AddressLineFirst"SortExpression="AddressLineFirst"/>

                <asp:BoundFieldDataField="AddressLineSecond"HeaderText="AddressLineSecond"SortExpression="AddressLineSecond"/>

                <asp:BoundFieldDataField="City"HeaderText="City"SortExpression="City"/>

                <asp:BoundFieldDataField="State"HeaderText="State"SortExpression="State"/>

                <asp:BoundFieldDataField="Country"HeaderText="Country"SortExpression="Country"/>

                <asp:BoundFieldDataField="PinCode"HeaderText="PinCode"SortExpression="PinCode"/>

                <asp:BoundFieldDataField="PhoneNo"HeaderText="PhoneNo"SortExpression="PhoneNo"/>

                <asp:BoundFieldDataField="EmailAddress"HeaderText="EmailAddress"SortExpression="EmailAddress"/>

            </Columns>

        </asp:GridView>

 

    </form>

</body>

</html> 



3. Data caching:

Data caching allows caching of data as objects for multiple pages in an application. As  the cached objects are  available at application level so it is also called Application level caching. The cached data for objects are available until the application is restarted. Data caching allows developers to programmatically maintain data across requests. 

Example:

1.       Create a login Form 
LoginForm.aspx

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="LoginForm.aspx.cs"Inherits="DataCachingSam.LoginForm"%>

    <!DOCTYPEhtml>

 

    <htmlxmlns="http://www.w3.org/1999/xhtml">

    <headrunat="server">

        <title></title>

    </head>

    <body>

        <formid="form1"runat="server">

 

        <div> 

        User Name:-<asp:TextBoxID="txtUserName"runat="server"></asp:TextBox> 

    <br/> 

    <br/> 

        Password:-<asp:TextBoxID="txtpwd"runat="server"></asp:TextBox> 

    <br/> 

            <asp:ButtonID="btnSubmit"runat="server"Text="submit"OnClick="btnSubmit_Click"/>

        </div>

        </form>

    </body>

    </html> 



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace DataCachingSam

{

    publicpartialclassLoginForm : System.Web.UI.Page

    {

        protectedvoid Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protectedvoid btnSubmit_Click(object sender, EventArgs e)

        {

            Cache["UserName"] = txtUserName.Text;

            Cache["Pwd"] = txtpwd.Text;

            Response.Redirect("DataCaching.aspx");

        }

    }

}


Updated 23-Feb-2015

Leave Comment

Comments

Liked By