CACHING
CACHING IS
THE TECHNIQUE IN WHICH WE CAN STORE THE RENDERD OUTPUT IN THE CACHE MEMORY. IT
IS A SERVER SIDE TECHNIQUE WHICH HELPS WHEN WE PROCESS THE DATA, IF WE HAVE A
PAGE WHICH REQUIRED FOR MORE TIME SO WE CAN KEEP IT IN CACHING SO THERE IS NO
REQUIRED FOR PROCESSING AGAIN AND AGAIN
CACHING CAN BE DEFINED INTO THE FOUR PARTS
1.
PAGE
CACHING OR OUTPUT CACHING
2.
FRAGMENT
CACHING OR USER CONTROL OR PARTIAL CACHING
3.
DATA CACHING
4.
SQL
DATASOURCE CACHING ( WHICH IS IN 2.0 FRAMEWORK)
PAGE
CACHING
IN PAGE CACHING WE CAN
STORE THE OUTPUT OF ANY PAGE IN CACHE MEMORY, HERE WE CAN STORE THE RANDERED
OUT PUT OF THE PAGE
Ø WE CAN USE THIS TECHNIQUE WHERE DATA IS NOT CHANGING
FREQUENTLY
Ø HERE CLIENT IS NOT WATCHING THE REAL DATA
Ø FOR THE NEXT REQUEST FOR THE PAGE THIS RANDERED OUTPUT
IS PROVIDED TO THE CLIENT WITHOUT DOING PROCESSING FOR THIS REQUEST
Ø THIS RANDERED OUTPUT CAN BE STORED IN CLIENT SIDE AND
BETWEEN SERVER SIDE
SERVER
IF WE STORED THE RANDERED OUTPUT AT SERVER
SIDE, THAN IT CAN BE ACCESSED ALL OVER THE WORLD, THERE IS AN PROBLEM OF
NETWORK TRAFFIC. BUT IT SAVE TIME
CLIENT SIDE
HERE WE STORED THE RANDERED OUTPUT AT CLIENT
SIDE, IT SAVE THE NETWORK TRAFFIC PROBLEM, BUT IT IS NOT SHAREABLE
DOWNSTREAM
HERE WE CAN SPECIFIED LOCATION OF YOUR CACHED
TO STORED. WE CAN SET THE LOCATION
1.
ANY
2.
CLIENT
3.
SERVER
4.
DOWNSTREAM
Ø BY DEFAULT IT IS ANY
Ø BUT WE SHOULD STORE THE RANDERED OUTPUT AT SERVER SIDE
WHICH IS PREFERRED FIRST AND WHICH IS BENEFETIAL
2. FRAGEMENT CACHING OR USER CONTROL
IN THIS CACHING TECHNIQUE THE PARTICULAR PORTION OF PAGE IS STORED IN
CACHE. REMAING PORTION IS DYNAMIC PAGE
>> TO USE THIS WE HAVE
TO CREATE USER CONTROLS THE EXTENSTION .ASCX
DYNAMIC
PAGES
|
THIS
PORTION IS STORED IN CACHE MEMORY( SUPPOSE WE HAVE CREATE A GRID VIEW A USER CONTROLS , THAN IT WHOLE
PAGE IS PROCESS EVERY TIME BUT GRIDVIEW IS FETCHED DATA FROM CACHE MEMORY
FOR EACH REQUEST)
|
THERE IS AN REVERSE OF THIS CACHING AND THIS TECHNIQUE IS CALLED POST
CACHE SUBSTITUTION
POST CACHE SUBSTITUTION
IT MEANS
THAT ONE PART OF THE PAGE WILL BE STATIC MEANS STORED IN CACHE MEMORY AND OTHER
PORTION WILL BE DYNAMIC
>>>
TO IMPLEMENTS THIS THERE IS AN CONTROLS IN TOOLBOX CALLED SUBSTITUTION CONTROLS
SUBSTITUTION
CONTROLS
IT CAN PROCESS ANY PARTICULAR CONTROLS ON
SERVER SIDE
STATIC
PAGE
|
DYNAMIC
PAGE
|
3. DATA CACHING
IN THIS
TECHINIQUE WE CAN STORE THE OBJECT LIKE DATASET
IN CACHE MEMORY, HERE WE CAN CONSTRUCT OUR OWN DATA IN SERVER CACHE AND
CAN CREATE DEPENDENCY ON TIME AND ON ANY FILE.
Ø THIS IS IMPLEMENT THROUGH THE CODING
Ø HERE WE CAN CREATE DIFFERENT DEPENDENCY ON TIME, CACHE
MEMORY OF OUR OBJECT
Ø DATA CACHING IS ALWAYS STORED ON SERVER SIDE
4.SQL DATASOURCE CACHING
WHEN WE USE THE DATABINDING CONTROLS LIKE GRID AND DATA CONTROLS THAN SQL
DATASOURCE DO PROCESSING EVRY TIME WHEN WE DO POSTBACK
WHEN THE PAGE IS PROCESS
SQLDATASOURCE CAN BE STORED INTO CACHE MEMORY
VARY BY PARAM =” * “ PAGE WILL BE MAINTAINED EVERY TIME INTO CACHING
VARY BY PARAM=NONE PAGE WILL BE
MAINTAIN ONE TIME INTO CACHING
VARY BY HEADER=” “
ACCEPT HEADER WHICH TAKES THE
CULTURE FROM CLIENT BROWSER
D/F B/W CACHE AND APPLICATION OBJECT
APPLICATION OBJECT
1.
DATA
ALWAYS MAINTAIN
2.
WE
CAN MAKE THE DEPENDENCY OF DATA AND WE STORE THE DATA AT THE OBJECT AND
ALSO REMOVE THE OBJECT
|
CACHE
1.
CACHE
DATA HAS NO GUARANTEE. SERVER CAN REMOVE THE CONTENT
2.
THERE WE CAN NOT MAKE THE DEPENDENCY OF
THE DATA
3.
CACHE
IS REMOVED WHEN LOW MEMORY
|
1. PAPAGE CACHING/OUTPUT CACHING
Ø BY DEFAULT TWO VESION (COPY) IS CREATED
ü ONE FOR THE GET REQUEST
ü SECOND FOR THE POST REQUEST
NO NEED TO DO CODING JUST WE HAVE TO ADD DIRECTORY
OPEN A NEW WEBSITES
HERE FIRSTLY CODE IS CONVERTED IN HTML FORMAT THAN STORED IN CACHE MEMORY
protected void Page_Load(object
sender, EventArgs e)
{
Response.Write("<h1>"
+ DateTime.Now.ToLongTimeString() + "</h1>");
}
protected void Button1_Click(object
sender, EventArgs e)
{
Response.Write("<h1>"
+ DateTime.Now.ToLongTimeString() + "</h1>");
}
TO STORE IN CACHE
OPEN
SOURCE CODE OF YOUR PAGE
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
WRITE THIS
<%@ OutputCache
Duration="25"
Location="Server"
VaryByParam="none"
%>
WHEN YOU WILL RUN
THIS PROGRAM AT GET REQUEST YOU WILL SEE THE DATETIME ON THE FORM AND WHEN YOU
CLICK ON BUTTON AGAIN YOU POSTBACK YOU WILL FIND THE SAME TIME TILL 25 SECONDS
(CACHE TIME DURATION ) BECAUSE YOU HAVE
PUT YOUR PAGE IN CACHE MEMORY AND AFTER 25 SECONDS OUTPUT WILL CHANGED
protected void Page_Load(object
sender, EventArgs e)
{
SqlDataAdapter
da = new SqlDataAdapter("select * from stu", "Data Source=.;Initial Catalog=ishan;Integrated
Security=True");
DataSet
ds = new DataSet();
da.Fill(ds, "st");
GridView1.DataSource = ds;
GridView1.DataBind();
}
<%@ OutputCache
Duration="25"
Location="Server"
VaryByParam="*"
%>
2.FRAGMENT CACHING OR PATIAL CACHING OR USER
CONTROL CACHING
FOR A
PARTICULAR PORTION OF THE PAGE TO STORE THEM IN CACHE MEMORY,AND IT CAN BE DONE
BY USER CONTROLS.
ADD A NEW WEB USER CONTROLS
using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
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;
using
System.Data.SqlClient;
public partial class WebUserControl : System.Web.UI.UserControl
{
SqlDataAdapter
da;
DataSet
ds;
protected void Page_Load(object
sender, EventArgs e)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
ds = new
DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Label1.Text = DateTime.Now.ToLongTimeString();
}
}
Than
come in source CODE OF THE WEB USER
CONTROLS
<%@ Control Language="C#"
AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl"
%>
WRITE THIS AFTER IT
<%@ OutputCache
Duration=15 VaryByParam=none %>
THAN DRAG THIS WEBUSER CONTROL IN TO YOUR PAGE
DEFAULT.ASPX
HERE SHARED =TRUE/FALSE
TRUE
DO TRUE TO SHARED THE OUTPUT OF THE USERCONTOLS
CACHE OUTPUT FOR ALL PAGES
FALSE
IT IS BY
DEFAULT SET
ALL PAGES WILL NOT SHARE THE INSTANCE OF THIS USERCONTROLS,
EACH PAGE CREATE IT’S NEW OUTPUT STORE USERCONROLS IN CACHE, MEANS ON EACH PAGE
THIS WEB USERCONTROLS SHOW A DIFFERENT-2 VALUES
ADD A
DROP DOWN LIST CONROLS IN WEB USER CONTROLS AND
THAN CLICK ON SMART TAG >>> SELECT
EDIT TO ADD ITEMS >> ADD THREE ITEMS LIKE A, B AND C IN DROP DOWN LIST
AND THAN
CLICK ON SMART TAG AND CLICK ON ENABLE POST BACK
THAN
COME ON SOURCE CODE OF WEB USER CONTROLS
<%@ Control Language="C#"
AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl"
%>
WRITE
THIS AFTER TO VARY THE RESULT BY CONTROL DROPDOWN LIST
<%@ OutputCache Shared="true" Duration=15 VaryByControl="DropDownList1" %>
POST CACHE SUBSTITUTION
TO KEEP A PARTICULAR SECTION OF A PAGE DYNAMIC
AND REMAING PORTION IS STATIC
HERE WE
USE THE SUBSTITUTION CONTROL FROM TOOLBOX
THAN CREAT A METHOD ON DEFAULT 3 PAGE
public partial class Default3 : System.Web.UI.Page
{
public static string abc(HttpContext hc)
{
return DateTime.Now.ToLongTimeString();
}
AND TAKE
A LABEL FOR FORM AND TAKE A BUTTON
THAN WRITE THIS ON PAGE LOAD
protected void Page_Load(object
sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString()
+ "<br/>";
// WRITE THIS IF YOU WANT TO SELECT THE METHOD BY
CODING NOT BY PROPERTY
Response.WriteSubstitution(new HttpResponseSubstitutionCallback(abc));
}
}
OR
IF YOU WANT TO SELECT BY PROPERTY
THAN WRITE CLICK ON SUBSTITION
CONROLS >>>> SELECT PROPERTY
WRITE
NAME OF YOUR METHOD HERE
THAN COME ON SOURCE CODE OF YOUR PAGE
AND
WRITE THIS
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default3.aspx.cs"
Inherits="Default3"
%>
<%@ OutputCache Duration="10" VaryByParam="none" %>
3.DATA CACHING
WITH THE HELP OF DATA CACHING
WE CAN STORE OUR OWN DATA IN TO THE CACHE MEMORY
NOW PUT THE DATAGRID VIEW INTO CACHE, HERE THE
VALUES IS STORED IN TO THE CACHE ON THE BASIS OF KEY INTERNALLY IF FOLLOWS A DICTIONARY
ARCHITECTURE, OBJECT DATA CACHING IS ALWAYS ON SERVER SIDE,
HERE THE DATA IS STORED IN TO THE CACHE MEMORY
IN THE FORM OF OBJECT , SO WHEN WE WANT TO RETRIEVE THE DATA FROM THE CACHE WE
HAVE TO DO TYPECASTING
using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
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;
using
System.Data.SqlClient;
using
System.Web.Caching; // ADD THIS
NAMESPACE TO USE
.
CACHING CLASSES
public partial class DATACACHING : System.Web.UI.Page
{
SqlDataAdapter
da;
DataSet
ds = new DataSet();
protected void Page_Load(object
sender, EventArgs e)
{
if
(Cache["data"] == null)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
Cache.Insert("data", ds);
Response.Write("data is inserted in to cache memory");
}
else
{
ds = (DataSet)Cache["data"];
Response.Write("data from cache");
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
TO REMOVE DATA FORM CACHE MEMORY
protected void REMOVE_Click(object
sender, EventArgs e)
{
Cache.Remove("data");
Response.Write("data
is removed from cache");
}
TO CREATE DEPENDENCY
1.TIME DEPENDENCY
TO MAKE
THE DEPENDENCY OF THE DATA WHICH IS STORED IN CACHE MEMORY ON TIME
using
System.Web.Caching;
public partial class DATACACHING : System.Web.UI.Page
{
SqlDataAdapter
da;
DataSet
ds = new DataSet();
protected void Page_Load(object
sender, EventArgs e)
{
if
(Cache["data"] == null)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
Cache.Insert("data", ds,null,DateTime.Now.AddSeconds(10),System.Web.Caching.Cache.NoSlidingExpiration);
Cache.Insert("data", ds, null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromSeconds(5));
Response.Write("data is inserted in to cache memory");
}
else
{
ds = (DataSet)Cache["data"];
Response.Write("data from cache");
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
FILE
DEPENDENCY
IF YOU WANT TO MAKE THE DEPENDENCY OF THE DATA
ON A FILE, LIKE IF SOMEONE DO SOME CHANGES IN A FILE ON WHICH YOUR CACHE IS
DEPENDENT THE DATA WILL REMOVE
AUTOMATICLLY FROM CACHE MEMORY
using
System.Web.Caching;
public partial class DATACACHING : System.Web.UI.Page
{
SqlDataAdapter
da;
DataSet
ds = new DataSet();
protected void Page_Load(object
sender, EventArgs e)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
if
(Cache["data"] == null)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
CacheDependency
cd = new CacheDependency("C:\\pawan.xml");
Cache.Insert("data", ds,cd);
Response.Write("data is inserted in to cache memory");
}
else
{
ds = (DataSet)Cache["data"];
Response.Write("data from cache");
}
GridView1.DataSource = ds;
GridView1.DataBind();
MULTILE FILE DEPENDENCY
TO CREATE DEPENDECY ON MORE THAN ONE FILES
using
System.Web.Caching;
public partial class DATACACHING : System.Web.UI.Page
{
SqlDataAdapter
da;
DataSet
ds = new DataSet();
protected void Page_Load(object
sender, EventArgs e)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
if
(Cache["data"] == null)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
CacheDependency
cd = new CacheDependency("C:\\manoj.txt");
CacheDependency
cd1 = new CacheDependency("C:\\ravi.txt");
CacheDependency
cd2 = new CacheDependency("C:\\hi.txt");
AggregateCacheDependency
acd = new AggregateCacheDependency();
acd.Add(cd);
acd.Add(cd1);
acd.Add(cd2);
Cache.Insert("data", ds,acd);
Response.Write("data is inserted in to cache memory");
}
else
{
ds = (DataSet)Cache["data"];
Response.Write("data from cache");
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
XML FILE DEPEDENCY
TO
CREATE THE DEPENDENCY ON THE XML FILE
using
System.Web.Caching;
public partial class DATACACHING : System.Web.UI.Page
{
SqlDataAdapter
da;
DataSet
ds = new DataSet();
protected void Page_Load(object
sender, EventArgs e)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
if
(Cache["data"] == null)
{
da = new
SqlDataAdapter("select
* from student", "Data
Source=.;Initial Catalog=ishan;Integrated Security=True");
da.Fill(ds);
ds.WriteXml("C:\\pawan.xml");
CacheDependency
cd = new CacheDependency("C:\\pawan.xml");
Cache.Insert("data", ds,cd);
Response.Write("data is inserted in to cache memory");
}
else
{
ds = (DataSet)Cache["data"];
Response.Write("data from cache");
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
4.SQL DATABASE CACHING
Run this command in visual studio command
propmpt to make the cache dependency on your own database
D:/>
aspnet_regsql –S . –E –ed –d ishan –et –t ishu
Here
ishan is name of your database
And –e is for integrated security true
And –s for server name
AND –ED IS FOR ENABLE DATABASE
HERE –ET IS FOR ENABLE TABLE
-T FOR TABLE
AND ISHU IS TABLE NAME
THIS WILL ADD A TABLE IN YOUR DATABASE WITH
NAME
(AspNet_SqlCacheTablesForChangeNotificationAspNet_SqlCacheTablesForChangeNotification)
WHEN SOMEONE DO CHANGES ON THE TABLE THAN IT
WILL ADD A KEY IN THIS TABLE ,
SO HERE
LOOKUP POLLINGG SYSTEM IS RUNNING
FIRST TAKE A GRID VIEW CONTROLS AND A BUTTON
THAN OPEN WEB.CONFIG FILE AND WRITE THIS
<appSettings/>
<connectionStrings>
<add
name="con" connectionString="Data Source=.;Initial
Catalog=ishan;Integrated Security=True"/>
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency
enabled="true">
<databases>
<add
name="master" connectionStringName="con" pollTime="500"/>
</databases>
</sqlCacheDependency>
</caching>
THAN OPEN YOUR PAGE LOAD OF YOUR PAGE
using
System.Web.Caching;
public partial class _Default : System.Web.UI.Page
{
DataSet
ds;
protected void Page_Load(object
sender, EventArgs e)
{
if
(Cache["data"] == null)
{
SqlDataAdapter
da = new SqlDataAdapter("select * from ishu", ConfigurationManager.ConnectionStrings["con"].ConnectionString);
DataSet
ds = new DataSet();
da.Fill(ds);
SqlCacheDependency
cd = new SqlCacheDependency("master", "ishu");
Cache.Insert("data", ds, cd);
Response.Write("data inserted into cache");
}
else
{
ds = (DataSet)Cache["data"];
Response.Write("data is coming from cache memory");
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
THAN OPEN SOURCE CODE OF YOUR
PAGE
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache
Duration="20"
SqlDependency="master:ishu"
VaryByParam="none"
%>
NOW
THIS IS DONE IF ANY ONE DO SOME CHANGES ON ANY TABLE THAN IT WILL REMOVE
THEDATA FROM CACHE MEMORY
No comments:
Post a Comment