December 22, 2014
Hot Topics:

Improving Your Web App's Performance with Aggressive Data Caching

  • October 15, 2004
  • By Mike Amundsen
  • Send Email »
  • More Articles »

Anyone who has worked on Web applications that use dynamic content knows that data access can be a real bottleneck. One of the nice features in the ASP.NET runtime is its data-caching services via the System.Web.Caching namespace. The challenge most developers face is how to plan ahead for data caching and how to effectively implement it in a way that doesn't over-complicate the code. This article offers a quick review of the features and functions of ASP.NET's data caching. You'll also learn how to build a very simple data-caching "plug-in" assembly in less than 100 lines of code. Finally, you'll see how you can easily use this assembly in your ASP.NET pages to help boost the performance of your Web apps without adding a lot of lines of code.

The Basics of ASP.NET's Data Caching

ASP.NET's data caching provides an in-memory, application-scoped, thread-safe "bucket" that can hold any serializable object or object collection. You can use this feature to hold the results of expensive database queries, large XML documents, and even simple arrays and custom objects you design yourself. Then, during the lifetime of your Web application, you can recall this data from the cache instead of having to go back to the original source every time. Even better, the data cache has features that allow you to set the lifetime of an item in the cache. This enables you to, in some cases, automatically refresh the cache when the underlying data changes.

So, the challenge is to turn this high-powered object into an easy-to-use tool in your Web application toolbox. What you need is a single class that encapsulates the most commonly used features of ASP.NET's data caching and one that provides shortcuts for inserting, refreshing, and recalling data from the cache.

Designing the Cache Utility Class

To make it easier to handle cached items, you can use a simple class to encapsulate key functions and simplify reading, writing, listing, and removing items in the cache. Below is a list of the methods and the associated arguments for each method:

public static string ListCache()
public static string ListCache(string mask)

public static void ClearCache()
public static void ClearCache(string mask)

public static object GetCacheItem(string key)

public static void DropCacheItem(string key)

public static void SetCacheItem(string key, object data)
public static void SetCacheItem(string key, object data,
   CacheDependency dependencies)
public static void SetCacheItem(string key, object data,
   int seconds)
public static void SetCacheItem(string key, object data,
   int seconds, int slidingSeconds)

GetCacheItem and DropCacheItem are very simple—they return or remove a selected item. ListCache will return a list of the items in the cache. One version of the method allows you to pass a filter string to control which items are returned. ClearCache will remove either all the items in the cache or just the ones that match the filter string.

The really interesting method is SetCacheItem. It allows you to add an item to the cache and optionally set either a dependency (usually a file name) or a set of expiration values (the maximum life of the object in seconds and/or the maximum idle time of the object in seconds). Dependencies work like this: You can link a cached item to an external item such as a disk file. If that disk file changes, the item is automatically removed from the cache. This is how ASP.NET tracks changes in your WEB.CONFIG files.

Expiration policies are a bit trickier. You can set the lifetime of an object (in this class in seconds). If the object has been in the cache for more than the lifetime, it is automatically removed. You also can set the idle-time of the object (in this class in seconds). If an object has been left idle, meaning no one has requested it, for more than the indicated seconds, it is removed from the cache. The following is the coding for the SetCacheItem method that deals with lifetime and idle time:

public static void SetCacheItem(string key, object data,
   int seconds, int slidingSeconds)
{
   if(slidingSeconds>0)
      HttpContext.Current.Cache.Insert(key,data,null,
         System.DateTime.MaxValue,TimeSpan
               .FromSeconds(slidingSeconds));
   else
      HttpContext.Current.Cache.Insert(key,data,null,
         System.DateTime.Now.AddSeconds(seconds),
         TimeSpan.FromSeconds(slidingSeconds));
}

The ListCache method returns a simple list (in HTML format) of all the items in the cache. The only challenge to listing items from the cache is that "walking" the Cache collection requires the use of an Enumerator.

public static string ListCache(string filter)
{
   StringBuilder sb = new StringBuilder();
   string key="";

   System.Collections.IDictionaryEnumerator en = 
      HttpContext.Current.Cache.GetEnumerator();
   while(en.MoveNext())
   {
      key=en.Key.ToString();
      if(filter.Length!=0 && key.IndexOf(filter)!=-1)
         sb.AppendFormat("{0}",key);
      if(filter.Length==0)
         sb.AppendFormat("{0}",key);
   }
   return sb.ToString();
}

This summarizes the basic class that provides easy access to the ASP.NET Cache class. The next step is to create a helper class that uses the basic methods to provide the needed high-level access to the cache collection.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel