Home > DeveloperSection > Blogs > Caching in ASP.NET

Caching in ASP.NET


.NET C#  ASP.Net 
Ratings:
0 Comment(s)
 840  View(s)
Rate this:

Caching in ASP.NET

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:

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

 

<!DOCTYPE html>

 

<%@ OutputCache Duration="60" VaryByParam="state" %>

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

<head runat="server">

    <title></title>

 

</head>

<body>

    <form id="form1" runat="server">

        <i>Last generate on:</i>

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

          <h3>

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

        <b>Authors by State:</b>

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

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

            <tr>

                <td>

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

                <td>

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

                <td>

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

                <td>

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

                <td>

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

                <td>

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

                <td>

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

                <td>

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

            </tr>

        </table>

        <asp:SqlDataSource ID="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:QueryStringParameter Name="state" QueryStringField="state" DefaultValue="CA"/>

            </SelectParameters>

        </asp:SqlDataSource>

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

            <Columns>

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

                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />

                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />

                <asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" SortExpression="DateOfBirth" />

                <asp:BoundField DataField="FatherName" HeaderText="FatherName" SortExpression="FatherName" />

                <asp:BoundField DataField="AddressLineFirst" HeaderText="AddressLineFirst" SortExpression="AddressLineFirst" />

                <asp:BoundField DataField="AddressLineSecond" HeaderText="AddressLineSecond" SortExpression="AddressLineSecond" />

                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />

                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />

                <asp:BoundField DataField="PinCode" HeaderText="PinCode" SortExpression="PinCode" />

                <asp:BoundField DataField="PhoneNo" HeaderText="PhoneNo" SortExpression="PhoneNo" />

                <asp:BoundField DataField="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

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

    <!DOCTYPE html>

 

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

    <head runat="server">

        <title></title>

    </head>

    <body>

        <form id="form1" runat="server">

 

        <div> 

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

    <br /> 

    <br /> 

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

    <br /> 

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

        </div>

        </form>

    </body>

    </html>

 

 

 

 

LoginForm.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace DataCachingSam

{

    public partial class LoginForm : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void btnSubmit_Click(object sender, EventArgs e)

        {

            Cache["UserName"] = txtUserName.Text;

            Cache["Pwd"] = txtpwd.Text;

            Response.Redirect("DataCaching.aspx");

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick