ASP.NET,Data Caching, Output Caching, Fragment Caching, Post Cache Substitution

Caching is a technique used in various aspects of computing to increase performance by decreasing access times for obtaining data. This is accomplished by retaining frequently accessed data within memory.



ASP.NET makes available three different types of caching, which, when used properly, can greatly increase the overall performance of your application.
These types are as follows:

_ Output Caching
_ Fragment Caching
_ Post Cache Substitution
_ Data Caching

Output Caching:
Output caching basically caches the entire content of an output Web page.This can be very useful when the content of your pages changes very little.
Output caching provides the capability to cache response content generated from dynamic pages for the purpose of increasing application performance. This form of caching should be applied when the content of your page is somewhat static.
Various options can be set for output caching including the duration. In order for a page to be cached using output caching, it must have a valid expiration or validation policy.These options can be set either through the Using the @ OutputCache Directive

When the @ OutputCache directive is used at the top of the page,ASP.NET basically uses the Page.InitOutputCache method to translate the directive parameters into HttpCachePolicy class methods. To set the expiration of a page you intend to cache, you can use the following code at the top of the page:

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

This sets the cache duration for this page to 60 seconds.
The VaryByParam attribute is one of three attributes used to control caching of multiple pages by the @ OutputCache directive.These attributes are as follows:

_ VaryByParam
_ VaryByHeader
_ VaryByCustom

When ASP.NET generates the content of your page, the output can vary based on values that have been passed to the page. By using the VaryByParam attribute, you can control the caching of these pages based on a GET query string or POST parameters. By specifying the GET query string parameters or POST parameters using this attribute, each request received for that parameter using a different value will be cached.
For example, if you specified the “id” GET query string parameter, each request received with a different name value will be cached separately.The syntax for setting a 60-second cache with the “id” parameter is as follows:

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

Fragment Caching:
Fragment caching, which is new in ASP.NET, allows for the caching of portions of your output page.This is an excellent improvement in caching technique, and is best used when your application’s output page has content that changes constantly in addition to content that changes very little.While this method does not provide as much of a performance increase as output caching, it does increase performance for applications that would formerly have been unable to use any caching at all due to the strict requirements of output caching.
In some pages we have some dynamic content that needs to be updated regularly, as well as content that remains relatively static, For this we have a concept called Fragment caching.
This enables you to break your page into separate sections (fragments) that can be cached individually with their own caching options.

Using fragment caching is very similar to output caching. In fact, you call it in the same way as output caching by using the @OutputCache directive. Fragment caching is implemented by separating user controls out of your main page, and assigning caching parameters to each user control.This gives you a greater level of control over which portions of your page are cached.

For example take a user control and give @outputcache directive to that control.

     <%@ Control Language="VB" AutoEventWireup="false" CodeFile="fragmentCacheControl.ascx.vb" 
     Inherits="fragmentCacheControl" %>
    <%@ OutputCache Duration="60" VaryByParam="none"%>
'And add label control and in code-behind file lbl1.Text = "Time in UserControl: " & DateTime.Now Register this user control in web page like below.
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Fragementcaching.aspx.vb"
     Inherits="Fragementcaching" %>
    <%@ Register TagPrefix="fragement" TagName="control" Src="~/fragmentCacheControl.ascx" %>



    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <fragement:control ID="usercontrol1" runat="Server" /> 
        </div>
        </form>
    </body>
    </html>
 

And in code behind file write
Response.Write("Time in Main page: " & DateTime.Now)

If you execute this page two times are displaying. One from main page and another from user control. If you refresh the page, then you observed that main page time only changed but not user control time, because user control time is cached.


Post Cache Substitution:
Post Cache Substitution is the exact opposite to the fragment caching, where the page is cached and portion of the page is dynamic.
Post cache substitution achieved through the Substitution control, which works differently from other form of caching. Still you can use the @outputcache directive even though some content in the page is dynamic.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="postCacheSubstitution.aspx.vb"
     Inherits="postCacheSubstitution" %>
    <%@ OutputCache Duration="60" VaryByParam="none"%>
    <!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>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Substitution ID="dynamic_contenet1" MethodName="staticTime"  runat="Server" /> 
        </div>
        </form>
    </body>
    </html>

To achieve post cache substitution using Substitution control, you need to call shared method(in c# static method) for “Substitution” control.


    Public Shared Function staticTime(ByVal mycontext As HttpContext) As String
        Return "Time from Substitution control: " & DateTime.Now.ToString
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write("Time from Page(caching): " & DateTime.Now.ToString)
    End Sub


If you executes the above code you can find that time in substitution control changed, but not page time, because it is cached.



Data Caching:
Data caching, also new in ASP.NET, provides the ability to cache individual objects. Placing objects into the cache in this manner is similar to adding items to a dictionary. By using a simple dictionary-style interface, this method makes for an easy-to-use temporary data storage area while conserving server resources by releasing memory as cached objects expire.
There are three different methods that you can use to add data or objects to the cache.They all work in a very similar fashion, but offer different levels of control and usage.The cache method is used for fast and easy access to the data cache.The cache.add and cache.insert methods are used to give you a greater amount of control over the data that you cache.

        Cache("name") = "ABC"
        Cache.Insert("company", "xyz")
        Response.Write("name: " & Cache("name") & " &")
        Response.Write(" Country: " & Cache("name"))
 

Download source code here