Skip to main content

What is Caching in ASP.NET.

What is Caching?
Caching is a technique of persisting the data in memory for immediate access to requesting program calls. Many in the developer community consider caching as one of the features available to improve performance of Web applications.
Why Caching?

Consider a page that has list of Employee name, contact numbers and mail-Ids of existing employees of a company on an intranet accessible by all employees. This is very useful information that is available throughout the company and could also be one of the most accessed pages. The functionality of adding, updating or deleting is usually less intensive compared to more transaction-based systems like Purchase ordering, Voucher creation etc. Now in a normal scenario the process of querying database for each request is not cost-effective in terms of server resources, hence is lot better to cache or persist the data to avoid this costly loss of resources.

The .NET Advantage
ASP.NET provides the flexibility in terms of caching at different levels.
1. Page Level Output Caching

This is at the page level and one of the easiest means for caching pages. This requires one to specify Duration of cache and Attribute of caching.
Syntax: <%@ OutputCache Duration="60" VaryByParam="none" %>
The above syntax specifies that the page be cached for duration of 60 seconds and the value "none" for VaryByParam* attribute makes sure that there is a single cached page available for this duration specified.

2. Fragment Caching
Even though this definition refers to caching portion/s of page, it is actually caching a user control that can be used in a base web form page. In theory, if you have used include files in the traditional ASP model then this caching model is like caching these include files separately. In ASP.NET more often this is done through User Controls. Initially even though one feels a bit misleading, this is a significant technique that can be used especially when implementing "n" instances of the controls in various *.aspx pages. We can use the same syntax that we declared for the page level caching as shown above, but the power of fragment caching comes from the attribute "VaryByControl". Using this attribute one can cache a user control based on the properties exposed.
Syntax: <%@ OutputCache Duration="60" VaryByControl="DepartmentId" %>
The above syntax when declared within an *.ascx file ensures that the control is cached for 60 seconds and the number of representations of cached control is dependant on the property "DepartmentId" declared in the control.

3. Application Level Caching
With Page level Output caching one cannot cache objects between pages within an application. Fragment caching is great in that sense but has limitations by using user controls as means to do. We can use the Cache object programmatically to take advantage of caching objects and share the same between pages. Further the availability of different overloaded methods gives a greater flexibility for our Cache policy like Timespan, Absolute expiration etc. But one of the biggest takes is the CacheDependancy. This means that one can create a cache and associate with it a dependency that is either another cache key or a file.

Program 1: VarByParam=”none”

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



<%@ OutputCache Duration="5" 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>CACHING: Page Output Caching</title>



</head>



<body>



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



    <div>



       <asp:Label ID="lblTime" runat="server"></asp:Label>



    



    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />



    </div>



    </form>



</body>



</html>




Code Behind file:





using System.Web.Security;



using System.Web.UI;



using System.Web.UI.HtmlControls;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Xml.Linq;



 



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



{



    protected void Page_Load(object sender, EventArgs e)



    {



       



    }



    protected void Button1_Click(object sender, EventArgs e)



    {



        lblTime.Text = DateTime.Now.ToString();



    }



}




Output:



12









Program 2: VarByParam=”TextBox1”





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>



<%@ OutputCache Duration="8" VaryByParam="TextBox1" %>



<!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>Untitled Page</title>



</head>



<body>



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



    <div>



    <asp:Label ID="lblTime" runat="server"></asp:Label>



    



    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />



    </div>



    </form>



</body>



</html>






Code Behind file:





using System.Web.Security;



using System.Web.UI;



using System.Web.UI.HtmlControls;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Xml.Linq;



 



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



{



    protected void Page_Load(object sender, EventArgs e)



    {



 



    }



    protected void Button1_Click(object sender, EventArgs e)



    {



        lblTime.Text = DateTime.Now.ToString();



    }



}




Output:



Enter the Name in textbox and click the button yon will see the time



3





4





5



Note: Wile changing the name in textbox time will change



6

Comments

Popular posts from this blog

C++ Program to define a Class BOOK and accessing member function using its object.

  #include<iostream.h> #include<stdio.h> #include<conio.h> class BOOK { int BOOKNO; char BOOKTITLE[20]; float PRICE; void TOTAL_COST( int N) { float tcost; tcost=PRICE*N; cout<<tcost; } public : void INPUT() { cout<<" Enter Book Number "; cin>>BOOKNO; cout<<" Enter Book Title "; gets(BOOKTITLE); cout<<" Enter price per copy "; cin>>PRICE; } void PURCHASE() { int n; cout<<" Enter number of copies to purchase "; cin>>n; cout<<" Total cost is "; TOTAL_COST(n); } }; void main() { BOOK obj; obj.INPUT(); obj.PURCHASE(); getch(); }

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }