ASP.NET 4.0 Extensible Output Caching

A number of Exciting ASP.NET features are coming in the .NET Framework version 4.0. These features are included in Visual Studio 2010 release. This post explains about one of those features core services in ASP.NET 4.0


Extensible Output Caching

Output Caching enables the developers to store the HTTP response and page output in memory. Using Output Caching Feature ASP.NET can serve the subsequent requests more quickly by retrieving the data from memory. The limitation for this approach is It has to store the content always in memory

ASP.NET 4.0 having an extensibility option for output caching by configuring custom output-cache providers.  Output-cache providers can use any storage mechanism to persist HTML content. These can include local or remote disks, cloud storage and distributed cache engines.

  <outputCache defaultProvider="AspNetInternalProvider">
      <add name="DiskCache" 
          type="Test.OutputCacheEx.DiskOutputCacheProvider, DiskCacheProvider"/>

The default configuration settings for output-cache are as shown above.

You can create a custom output-cache provider as a class which derives from System.Web.Caching.OutputCacheProvider type.

You can also select different output-cache per control or per request. You can declaratively set the providerName attribute in a page or control directive as shown below.

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

After specifying the provider name in page directives then you also need to override a method in the Global.asax file to programmatically specify which provider to use for a specific request.

public override string GetOutputCacheProviderName(HttpContext context)
    if (context.Request.Path.EndsWith("Advanced.aspx"))
       return "DiskCache";
        return base.GetOutputCacheProviderName(context);

With this addition of output-cache feature to ASP.NET 4.0, you can now use this strategy for your websites.

Eg: The more traffic that you get for the pages in the website can use the in memory caching and the less traffic getting pages can use the disk cache.

Recommended option is using distributed cache so that the memory consumption is offloaded from front-end web servers.

Source: http://WWW.ASP.NET white paper on ASP.NET 4.0

Share this post :

%d bloggers like this: