Thursday, 29 March 2018

0231 CACHING Web Form





Catching is a technique to store frequent use data and information into the memory temporarily. As the information and data is storeed in the memory, if this is requested again, instead of requesting server, data and information is fetch from the memory . This makes an website  faster in performance . Some pages are dynamically generated , these are cost heavy CPU usage  and time consuming. If this is catched , CPU usage reduce and delivery faster to the client.

Catching still  possible
  • Object lifetimes not expired 
  • An application has not released memories 
  • Error or exception did not occurred  during catching. 

In this chapter we will go to several kind of catching technique
  • Catching web forms 
  • Catching part of forms  
  • Catching application data 
  • Monitor catching performance
Catching Webforms : A full webform can we catched  and store into the memory , when Requested  it is delivered as it was. The directive OutputCache  directive to catch full page. Duration is a parameter to set how  long (in second)  the pages should keep the memory .

Below is the example of the same.
Example 1


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %>
<%@ OutputCache Duration="60" VaryByParam="*" %>

<!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>
           </div>
             <asp:Button ID="Button1" runat="server" Text="Submit" />
         </form>
  </body>
</html>

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
            protected void Page_Load(object sender, EventArgs e)
             {
                  Response.Write(DateTime.Now.ToString("h:mm:ss tt"));
              }

}






                                            (Notice that Time is same after postback)


The above example catch the waveform for 20 second .Output cache has two parameter , duration and VaryByParam. Duration tell the time limit the page should be kept in the memory and VaryByParam allowed to multiple resource that could be kept in the memory.

Example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="false" %>
<%@ OutputCache Duration="60" VaryByParam="DropDownList1" %>

<!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:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>India</asp:ListItem>
                        <asp:ListItem>USA</asp:ListItem>
                        <asp:ListItem>UK</asp:ListItem>
                        <asp:ListItem>Brazil</asp:ListItem>
                        <asp:ListItem>Australia</asp:ListItem>
                  </asp:DropDownList>
          </div>
          <asp:Button ID="Button1" runat="server" Text="Submit" />
   </form>
</body>
</html>

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
              protected void Page_Load(object sender, EventArgs e)
             {
               Response.Write(DateTime.Now.ToString("h:mm:ss tt"));
             }
}


HttpCachePolicy class to control output caching, below is some commonly used method of

HttpCachePolicy class

ByValueParam : Catch multiple response from singular form.
ByValueHeader : Catch  multiple response from a single form based on http header. 
SetVaryByCustom: Catch it multiple response depending on string.  
SetExpires : How long the catch you would be in memory.  
SetCacheability : The location what to save  information  client host server  proxy server ect.


HttpCacheability is an enumaration for where the data / information will be stored.

* Option Stored Location
Any HttpCacheability.Server Any ,Client ,Proxy & Host
Client HttpCacheability.Private Client
DownStream HttpCacheability.Public Client or Proxy
Server HttpCacheability.ServerAndNoCache Host Server
Server and Client HttpCacheability.ServerAndPrivate Host Server & Client



Here is the use of HttpCacheability enumeration.The following syntax tell that Response will be catched for 60 second and it would be catched to Client or ProxyServer


Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);














No comments:

Post a Comment

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

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