Loading ...

Caching in ASP.NET | CodeAsp.Net

Caching in ASP.NET

(3441)
0
/5
Avg: 0/5: (0 votes)
Published: 6/13/2013 by mohit kumar

Introduction

In ASP.NET data access is very slow operation that can effect to your web applications performance. If you want to improve your web applications performance, your data access shouldn’t use database at all. So if you don’t use database then how you can manage your data. Here you can use Caching. You can cache your database records in memory. By taking advantages of caching, you don’t have to hit database again and again, because you can get your data from cache. Retrieving data from database is very slow but the other Hand retrieving data from the cache is very simple and fast.

There are three caching option in ASP.NET –

    1. Page Output Caching.
    2. Partial Page Output Caching.
    3. Data Caching.

Page Output Caching

  Page output caching cache the whole page. I mean to say that the page output caching enables you to cache the entire rendered contents of a page in memory. When you enable the page output caching, the entire page content cached. If you have static page, page output caching is very useful for page performance, because you can cache your page and your page don’t need to hit database again and again. Your page automatically gets the data from cache. Below I am giving a example of Page Output Caching -

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Caching.aspx.cs" Inherits="CachingDemo" %>
<%@ OutputCache Duration="20" VaryByParam="none" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblTest" runat="server"></asp:Label>
    </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;

public partial class CachingDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTest.Text = "Page First time Open.";
        
        if(IsPostBack)
        {
            lblTest.Text = "This is Postback.";
        }
    }

}

In this Example, I want to show you that when you add <%@ OutputCache %> directive, your page output caching enabled. There are two properties one is duration and one is VaryByParam. In above example I have set 20 Seconds of duration. Its mean that after every 20 seconds page will re-execute. The VaryByParam attribute tell that how the caching will be performed. If you have static page, you can set this to “NONE”. I also want to show you by this example that when page output caching enabled, page don’t go for postback it will be every time re-execute. Like Label text will never set as “This is Postback.”.  

Partial Page Output Caching

In above example, the page was static. If you want to put any randomly photo  advertisement and you want to that every time when page is refresh, it should be new photo then it can’t possible with page output caching. In this situation, we have one option and that is Partial Page Output Caching. By the benefits of partial page output caching, we can solve this problem. You can apply different caching rules to different areas of a page. For this we can create user controls and with the help of user controls we can show Dynamic data (random photo etc.)    

Data Caching

 We can cache an object also. We can store objects in memory and use in full applications. In ASP.NET every caching mechanisms use the Cache Object. We can cache String as well. Below I will give you one example that how you can cache String - 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>Name :</td>
            <td><asp:TextBox runat="server" ID="txtName"></asp:TextBox></td>
        </tr>
        <tr>
           <td><asp:Label ID="lblCache" runat="server"></asp:Label></td>
        </tr>
        <tr>
            <td><asp:Button runat="server" Text="Insert Cache" onclick="Unnamed2_Click" /></td>
            <td> <asp:Button runat="server" Text="Retrieve Cache" onclick="Unnamed3_Click" /></td>
        </tr>
    </table>
       
    </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;

public partial class DataCaching : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Unnamed2_Click(object sender, EventArgs e)
    {
        //textname value inserting in cache.
        Cache["UserName"] = txtName.Text;
    }
    protected void Unnamed3_Click(object sender, EventArgs e)
    {
        //verify that the cache value is not null
        if (Cache["UserName"] != null)
        {
            //set lblchache(label) value.
            lblCache.Text = Cache["UserName"].ToString();
        }
    }
}

 


Conclusion

 The caching is a rich and powerful service in ASP.NET. Depends on situations, we can use different caching in ASP.NET pages and create robust web applications. I hope this Article will be helpful for learners.      

 

Cheers!!

 

Comments (4)

mohanpk81
mohan raj said:

that was great

8/26/2010
 · 
 
by
smsubramaniyan

Nice post ya. It will help me sooo much. Can u elaborate the partial caching for advertisements like program?..

10/25/2010
 · 
 
by
mohit
mohit kumar said:

Thanks Mohan:)

and Subramaniyan I will write when I get free time..:)

10/25/2010
 · 
 
by
sandeep
sandeep  said:
i have many css files that take a lot of time to load every time , when page refreshed, is this possible to cache all the Css files
3/4/2013
 · 
 
by
  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image *

Top articles

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?