Pete Brown's irritatedVowel.com
   home    wallpaper    railroad    .net, c#/vb    photography    birds    psp tubes    home/wood projects    games    recipes  
about   |   privacy   |   guestbook   |   pete's blog          
AddThis Social Bookmark Button

Section Contents
Naming Conventions and Standards
Best Practices
Links
Demos
HTTPModule Example
ASP.Net Cache API
AddThis Social Bookmark Button

ASP.NET Cache API

I decided I wanted to fool around with the Cache API a little bit, so I added some optimizations to my Deep Linking HttpModule. I knew that one bottle neck in my HttpModule was going to be the bit where it locates and opens the XML file from the file system. On a site like mine, there is likely very little hit due to the low traffic, however, on a larger site, you may want to get every little bit of performance gain you can. The Cache API (Cache Object) in ASP.NET is one way to get that performance gain.

How it Works

When the Deep Linking HTTPModule needs to open a configuration file, it sends a request to the ConfigFileManager class by calling the static GetConfigFile method. The GetConfigFile method first checks to see if the XML document is in the cache and if it is, it returns it. If the XML document is not in the cache, it is loaded from the file system and then added to the cache.

Unlike session variables, the .NET Cache is shared between all sessions of the same ASP.NET application.

Code

Cache Code

I added another class to my DeepLinkingHandler project. The class is called ConfigFileManager, and has one static method named GetConfigFile that is used for retrieving the configuration file. If the configuration file is not already cached, it will be added to the application cache with the full file path as the key. Note that I also create a CacheDependency on the file. ASP.NET will monitor that file, and when it changes, the entry will be removed from the cache. That way you don't need to worry about refreshing the cache or anything mundane like that.

public static XmlDocument GetConfigFile(
       string configFileFullPath, HttpApplication app)
{
       XmlDocument doc ;
 
       doc = (XmlDocument)app.Context.Cache[configFileFullPath] ;
 
       if (doc == null)
       {
              // document was not found in the cache,
              //try to open it from the file system
 
              try
              {
                     // attempt to load the config file
                     doc = new XmlDocument() ;
                     doc.Load(configFileFullPath) ;
 
                     // if we got here, the config file loaded, so add it to
                     // the cache with the config file full path as the key
                     // Note that there is a cache dependency on the config
                     // file itself.  if it changes, the entry will be
                     // automatically removed from the cache.
                     app.Context.Cache.Insert(
                           configFileFullPath,  // key is file name.
                           doc,                 // object to add.
                           new CacheDependency(configFileFullPath) // dependency on the file.
                           ) ;
              }
              catch (FileNotFoundException)
              {
                     // swallow it. There is no deep link config

// file in the folder. 
        doc = null


return doc ;

}

Updated Deep Linking Module Code

In order to use the new ConfigFileManager class, I needed to make a few changes to the code in my HTTPModule. The changes are all located in one method : AuthorizedReferrer. 

bool AuthorizedReferrer(
       Uri requestURL, Uri referrerURL, HttpApplication app, out string redirectURL)
{
       bool authorized = true ;
       redirectURL = String.Empty ;
 
       // Get the folder used for the config file
       string requestFolder = GetFolderPathFromRequestURL(requestURL, app) ;
 
       // Use the ConfigFileManager to load the
       // config file from the cache or filesystem
       System.Xml.XmlDocument doc =
              ConfigFileManager.GetConfigFile(requestFolder + "YourConfigFile.config", app) ;
 
       if (doc != null)
       {
              // doc was not null, so a config file exists in this folder
 
              try
              {
                     string fileName = GetFileNameFromRequestURL(requestURL, app) ;
                     string referrerName ;
 
                     if (referrerURL == null)
                           referrerName = "" ;
                     else
                           referrerName = referrerURL.ToString() ;
 
                     System.Xml.XmlNode root = doc.DocumentElement ;
 
                     foreach (System.Xml.XmlNode fileNode in root.ChildNodes)
                     {
                           // see other article for code in here
                           // this code did not change
                     }
              }
              catch (Exception)
              {
                     authorized = true ;
              }
       }
 
       return authorized ;
}

Conclusion

The Cache API in .NET is extremely simple to use - no more difficult than working with the Session object.  As you can see, adding performance optimizations to an existing, modularized application required very few code changes.  However, I recommend that you design your application with the Cache API in mind up-front, so you can best reap the rewards of cached application data.

If you found this mini article to be helpful, be sure to drop me a note in my guestbook.

who's online