Next


Caching

• Caching (pronounced as “Cashing”), is one of the most powerful concept of ASP.NET.
• This is introduced to reduce the stress on web server.
• If your application is being executed simultaneously from two or more browser windows on the same system, or it is being executed simultaneously from two or more client systems on the network, or it is being executed simultaneous from two or more web clients on internet, on each post back of the page, a new request to the web server would be passed. So that, the entire page is to be refreshed. That means the contents of the web page would be re-loaded and the code will be executed every time. This causes heavy stress on the web server.
• For example, there is a submit button on your web page. If the submit button is clicked on one client, it is not a much stress on the web server. You suppose that the button is clicked from 20 client systems at-a-time. So that 20 requests will be passed to the web server. The web server should respond to the 20 clients. So this leads to slow-up the execution, because of heavy stress on the web server.
• To avoid this kind of problems, “Caching” concept is introduced.
• According to this, you can store the page and its output in the cache memory up to a certain time.
• If any request is sent to the web server, within the time limitation, the page will not be re-loaded / re-executed. No code will be executed. Simply the previously cached web page will be sent to the client as response.
• After completion of the specified time limit, the cached page will be removed automatically.
• If any request is sent to the web server after the time limit, the page will be re-loaded as usual.
Implementation:
• Write the following tag in the “Source” view, after Page directive:
<%@ OutputCache Duration="seconds" VaryByParam="none" %>

Demo on Caching

caching

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="20" VaryByParam="none" %>
…………………………………..
Default.aspx.cs
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Current Date and Time is: " + DateTime.Now.ToString();
    }