Implementing Custom Cache Providers in ASP.NET 4.0

A Path to Flexibility and Extensibility in ASP.NET Caching

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

Implementing Custom Cache Providers in ASP.NET 4.0

In earlier versions of ASP.NET, the object cache API had major constraints. You had to include the System.Web.dll assembly for your WPF or WinForms applications, and the API wasn't extensible.

ASP.NET 4.0 gives you the ability to implement your own cache providers in ASP.NET applications. To create a custom cache provider, you need to create a class that derives from the System.Web.Caching.OutputCacheProvider class. The OutputCacheProvider class in the System.Web.Caching namespace derives the ProviderBase class and contains abstract methods for designing and implementing your own custom Cache provider.

The System.Web.Caching namespace contains a list of classes and interfaces that let you provide support for caching in your ASP.NET applications. According to an article on MSDN (http://msdn.microsoft.com/en-us/library/system.web.caching(VS.100).aspx), "The System.Web.Caching namespace provides classes for caching frequently used data on the server. This namespace includes the Cache class, a dictionary that enables you to store data objects such as hash tables and data sets. It also provides expiration functionality for those objects, and methods that enable you to add and remove the objects. You can add the objects to the cache with a dependency on other files or cache entries. In that case, the Cache object can invoke a callback method to notify your application when an object is removed from the cache." An example of the OutputCacheProvider is shown in Figure 2.

To implement your custom cache provider, you should subclass the OutputCacheProvider class and then override its methods to write your custom code, as shown in Figure 3. Then you need to specify which cache provider to use in the application's web.config file, as shown in Figure 4.

You should also have your own custom class for storing objects in the cache. Note that the class should be marked with the \\[Serializable\\] attribute. Serialization is the name of the process that enables you to save an object in a persistent storage media for retrieval later. It does this by converting the object to be stored in a linear stream of bytes. The reverse process of serialization is called de-serialization. It is the process that enables you to reconstruct the object that was serialized. Listing 5 illustrates how you can implement a class for storing the cache data.

Refer to the CacheRegister class in Figure 5. Note that we have two data members, one for storing the expiration details and the other for storing the object that needs to be cached. You can also retrieve the cache provider name to be used programmatically. To do this, you need to override the GetOutputCacheProviderName(HttpContext context) method in the Global.asax file, as shown in Figure 6.

Moving Forward

In ASP.NET 4.0, Page Output Caching and Object Caching are implemented using the Provider Model. This enables you to use the Cache extensibility feature. You can now even implement your own Custom Cache Provider. In this article, I presented the new caching enhancements in ASP.NET 4.0, a focus on custom cache providers and how to implement them in your ASP.NET applications.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish