Output Caching : Post Cache Substitution Using Substitution Control

In this article, I will be explaining the Post Cache Substitution technique using the Substitution control for Output Caching in ASP.NET.

"We can make a significant performance improvement is ASP.NET using Output Caching" .

How is the above statement possible??

Explanation

If our Web Page is relatively static, the rendered contents can be Output Cached. Once we Output Cache our Web Page, then after the first load the rendered contents are cached in the server, the client or both (depending on settings) and then for subsequent Requests for a particular duration of time (depending on settings), there would be no need to go and create an instance of the Page class and ask it to run through its entire life cycle and then render the content.

Instead the contents will be grabbed from the buffered output and sent directly back to the client.

And thus we get significant Performance improvement by Output Caching the Page.

Note: For details regarding what (depending on settings) has been mentioned, please see the demo below.

WHAT IS POST CACHE SUBSTITUTION AND WHEN TO USE IT??

Now say there is a scenario in which our Web Page would be static for a particular duration of time (contents might be dynamically generated) for all the users accessing it, except a UI element in the page.

So we can't Output Cache the entire page except the UI element. Therefore in these kinds of scenarios, we can use a feature called "Post Cache Substitution".

As the name very well implies, here our page will also be Output Cached; but some portion of it (the substitution block) will be getting dynamically substituted.

For this dynamic substitution, the entire page life cycle will not be called; instead it will call a Static Function to get the particular content dynamically generated .

So, that particular content will be getting refreshed with each Single Request and the remaining portion of the page will come from the Output Cache.

So now let's see how to do Post Cache Substitution in ASP.Net using a demo:

1. Create a new project in Visual Studio 2010; "File" -> "New" -> "Project..." then Web and ASP.Net Empty Web Application and name it something, say PostCacheSubstitutionDemo.

2. Add a new item | Web form and name it something, say default.aspx.

3. Now let's implement the scenario. Say there is a Grid in our page which would read its value from the database. Our page also needs to show the current refreshed Time for each new Request.

4. For this sample Demo, we will Output Cache our entire page and Post Cache, we will substitute the current Time dynamically using a static function as explained in the preceding part.

5. We will be using a server side control (Substitution Control) to implement Post Cache substitution and to show the current time for each new Request while reading the other contents of the page from the Output Cache.

6. To quickly implement the GridView retrieval from the database, I am dragging and dropping the Employees table from the NorthWind database from the Server explorer.

Output-Caching1.jpg

7. Now configure your DataSource from the Design View of the Page (Default.aspx), to just show the few columns from the Employee table.

Output-Caching2.jpg

8. Now drag and drop the Substitution Control from the toolbox, which will help us to do Post Cache Substitution.

Output-Caching3.jpg


9. In our Scenario: We will use the Substitution control to show the current time which would be refreshed with each and every new Request.

10. Properties of the Substitution Control: See the method Name. In our case, as discussed above, this will be our static server-side function on the Page class to fetch data dynamically each time.

Output-Caching4.jpg

11. Let's define the static method "GetTime" in code behind in the Default.aspx.cs fles, as:

namespace PostCacheSubstitution
{
    public partial class Default : System.Web.UI.Page
    {
        Public static string GetTime(HttpContext contxt)
        {
            return "Current Time using Post Cache Substitution"DateTime.Now.ToLongTimeString();
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

  • The function GetTime needs to be static and should return a String type.
  • GetTime - takes a reference of the HttpContext object, so it does have all the information of the current user, but at the same time it cannot access the page; since it's just a static method and also the page does not exist while as it is being executed.

12. Now let's Output Cache our page, Default.aspx.

Add an OutputCache directive to the page:

<%@ OutputCache Duration="600" Location="Any" VaryByParam="none" %>

The following points are the settings for Output Caching as mentioned in the introductory section of this article:

  • Duration="600": specifies that after the first request, subsequent requests will be retrieved from the cache for the next 600 seconds.
  • Location="Any": means the page can be cached on the client browser or on the server.
  • VarbyParam="none": means there will be 2 unique instances of the page cached (one for GET, another for POST).

13. Let us also add one more label to the Web Page, which will also show the Current Time, but will not be Post Cache substituted. so that our results of the Post Cache substitution will be clearly evident from the output screen.

Default.aspx

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text ="Time:" + DateTime.Now.ToLongTimeString();
        }

14. So as per our demo, our entire page will be retrieved from the Output Cache after the first request, for 600 seconds, except the Time portion in the Substitution control.

15. Run the application.

Output-Caching5.jpg


16. Refresh the page (in the case of Subsequent Requests):

Output-Caching6.jpg

  • So now, since this a subsequent request after the first Page Load, the entire page was rendered from the Output Cache, except for the Substitution block, which shows the refreshed time.

I have attached the code which I have used for the sample demo.

Happy Learning!!


Similar Articles