• subscribe
February 23, 2010 12:00 AM

Implementing Custom Cache Providers in ASP.NET 4.0

A Path to Flexibility and Extensibility in ASP.NET Caching
Dev Pro
InstantDoc ID #124971

Caching is a great feature that reduces network latency and traffic by storing frequently used data in memory. An application can then retrieve this cached data quickly when needed. If used judiciously, caching can improve an application's performance considerably. Prior to ASP.NET 4.0, a Web application development framework from Microsoft that comes with Visual Studio 2010, you could not write custom cache providers; now you can design them with ease. In this article, I'll present a brief overview of caching and why it is used. Then I'll discuss the new caching enhancements in ASP.NET 4.0 and how you can use the cache API in ASP.NET 4.0 to write your own custom cache providers.

To implement the code examples discussed in this article, you should have Visual Studio 2010 Beta 2 or higher installed in your system. You also should have a good understanding of C# and ASP.NET basics.

What is Caching and Why Is It used?

Before we delve into the meat of the article, let’s take a quick tour of caching and how and why it is useful in applications. A cache is a region of memory that you can use to store objects for later retrieval. Retrieving objects from the cache is always faster than retrieving those objects from disk. You can use caching in ASP.NET applications in one of the following ways:

  • Page Output Caching—The output of the entire page is stored in the cache for retrieval or access by subsequent requests to that page
  • Page Fragment or Partial Page Caching—A specific portion of a Web page is stored in the cache rather than the complete page content
  • Data or Object Caching—This relates to storage and retrieval of data or objects in the cache

Figure 1 illustrates how you can configure Page Output Caching in your ASP.NET Web pages. In Page Fragment Caching or Partial Page Caching, a specific portion of the Web page is cached, as shown in the following example:

 <%@ OutputCache Duration="10" VaryByControl="Status" VaryByParam="*"%>

 In data caching, you need to use the cache API to store and retrieve objects to and from the cache, as shown in the following example:

 Cache ["Cache_Key"] = objectToStore; //To store data in the cache

 Object cachedObject = Cache ["Cache_Key"]; //To retrieve the cached data

New Caching Enhancements in ASP.NET 4.0

Caching is an area that has had major enhancements in ASP.NET 4.0. ASP.NET 4.0 provides excellent support for extensible Output Caching, a feature in ASP.NET that lets you cache the output of pages in memory so that subsequent requests to the same page can be fetched from the cache. This improves the application's performance greatly, particularly for Web pages that contain relatively stale data. You can use extensible output caching to add extensibility points to output caching and configure one or more custom output-cache providers.

ASP.NET 4.0 provides a new, extensible object cache API that supports both client and server applications. It provides a consistent API that you can use for programming against the following cache storages:

  • disk-based output caches
  • custom object caches
  • distributed object caches
  • cloud-based object caches



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here