Anda di halaman 1dari 3

Page OutputCache:

Take a label and a Button1 and write following code under button
protected void Button1_Click(object sender, EventArgs e)
{//button
Label1.Text = DateTime.Now.ToLongTimeString();
}

Execution: for every time u click u will see current time as output

Now goto .aspx page’s source and write

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

In “OutputCache” directive

“Duration” is 60 sec or 1min

VaryByParam NONE

This is called cache in memory

Now execute it:

The time will not changes by clicking on button for any no. of time ie. The
time is cache in memory and after 1min click on the button you will see the
change in time.(it removes the previous cache and stores new time in cache)

If u copy the url and open it in another browser u will get same result becoz
cache is common to all.

In the above example we did without parameter

Now let see with parameter cache

i.e VaryByParam=<value>

the value may be form textbox or any other controls or form varibles.

1. Open .aspx file and drag and drop a textbox1


2. Go to source code and edit the Output cache directive as
VarByParam=”TextBox1”

i.e <%@ OutputCache Duration="60" VaryByParam="TextBox1"%>

execute it:

1st input of textbox is “san” the time is 10:00:01

2nd input of textbox is “fff” 10:00:12


3rd input of textbox is “san” again this time the time will be 10:00:01 (the previously cached time of
parameter san in textbox).

See the disadvantages of Page OutPut caching:

We go for following method to over come the disadv. Of output caching that is DataCaching:

1. time based caching.


2. dependency based caching.

1st create following product.xml file in your project

<?xml version="1.0" encoding="utf-8" ?>


<productinfo>
<product>
<id>101</id>
<name>laptops</name>
<price>20000</price>
<qty>50</qty>
</product>
<product>
<id>102</id>
<name>desktops</name>
<price>15000</price>
<qty>10</qty>
</product>
</productinfo>

In .net we can use xml in

dataset

System.Xml name space

 Linq to xml

By using dataset.with the time based cache example

Drag and drop a lable(to provide info form where the data is coming form ie
form datasource(product.xml) or form cache),

button(to view by clicking it),

gridview(to view the data in it) and linkbutton(to refresh)in defalt.aspx


using System.Web;
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


{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (Cache["dsproducts"] == null)//cache variable dsproducts if not
existing and check the condition is null or not
{
ds = new DataSet();
ds.ReadXml(MapPath("product.xml"));//mappath to readxml file ie
product.xml
Cache["dsproduct"]=ds;//dataset ds values are stored in dsproduct
Label1.Text = "data from datasource";
}
else
{
ds = (DataSet)Cache["dsproduct"];
Label1.Text="data from cache";
}
GridView1.DataSource=ds;
GridView1.DataBind();
}

protected void LinkButton1_Click(object sender, EventArgs e)


{
ds = new DataSet();
ds.ReadXml(MapPath("product.xml"));//mappath to readxml file ie
product.xml
Cache["dsproduct"] = ds;//dataset ds values are stored in dsproduct
Label1.Text = "data from datasource";
GridView1.DataSource = ds;
GridView1.DataBind();

}
}

Anda mungkin juga menyukai