Tuesday, 1 November 2016

Asp.Net Catching

Catching is a technique to get frequently used data/ Information /pages instantly without re-processing web page from server.The object is stored in cache memory and deliver to browser when asked , this makes first operation & less load on server . To generate new HTML & data , is costly for server and it is also time consuming also , bit if stored in memory , retrieve data will be much more efficient than retrieve from database.That is the utility of catching .For example , we are developing an e commerce application , price list of item will not change daily , may be change once in a week.Now if we put the price list in catching , then e commerce site will work much faster.Generating a web page need several stage Page request, Page Initialization , Load , validation , rendering , unload new connection establishment with database , query execution , fetching data.But catching , just retrieve data from cache .Net framework in run time key-value map CLR object called cache .A catching is effective till
  • The catching lifetime , whcih has expired
  • The application has released memory
  • In any case ,catching fails
There are three type of catching in Asp.net Output catching , data catching , object catching. The scope of the data catching is within the application domain ,Catching comes under System.Web.Caching namespace .One point to remember , we can clear browser history to clear cache .Cache can be removed cache remove command

Cache.Remove("MyChache");


Output Catching : Output catching stored finally rendered HTML copy to cache memory.It is simple in implementation and very efficient in working .Output catching reduced response time and work still cache expires. @ OutputCache is the directive to declare output catching , several parmeter are there to maintain catching .For example

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ OutputCache Duration="100" VaryByParam="None" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
</body>
</html>

 C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.Sleep(1000);
        Response.Write("This page generated and cached at:"+System.DateTime.Now.ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

Runtime



 In the above example , page generation time is same , when we click on the button , the page is not re created in server , it is being server by catching after the very first time.

Data Caching : It is a technique to catch the data at the end . Data catching is effective as the previous condition satisfied .
  • The catching lifetime , which expires
  • The application released memory
  • In any case , catching does not occurred
Object Catching : Object catching is the catching a object from web page .Catching an object of an page is much more efficient than catch the entire page.Objects catching can catch complex object also .Still now we came to know , what is catching , type of catching and how catching works .Now we will go though parameter & configuration related to catching .First we will learn that .How we can control where catche can be stored .We can define it .The attribute is HttpCacheability.


HttpCacheability.Server :It tells that , the cache can be store any location /client /host/Proxy.

HttpCacheability.ServerAndNoCache :The response is catche at server & client and not any other location like proxy server.

HttpCacheability.Private :It tells that , the catche can be on the client only not on server or proxy server.

HttpCacheability.Public : Catching stored client & shared (proxy) server.
HttpCacheability.NoCache :Disable Catche

Sample Code :

    protected void Button1_Click(object sender, EventArgs e)
    {
         Response.Cache.SetExpires(System.DateTime.Now.AddDays(5);
         Response.Cache.VaryByHeaders["MyCache"]=true;
         Response.Cache.SetCacheability(HttpCacheability.Private);
    }

 Other than this , there are several ways to clear cache & control cache also . For example if we set 
<meta http-equiv="cache-control" content="no-catche" />

The catche will not work for this page , clear catching instantly for this page.
Previous , we learned about  @ OutputCache to set catche in HTML ,Now
@ OutputCache have some attribute


No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...