August 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Implement Caching to Give Your .NET Applications a Performance Boost

  • November 14, 2005
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Adding an Item to the Cache

In order to illustrate the advantages of the caching block, take a look at some sample code that creates an object named NameValuePair and adds that to the cache. (Click here to download the accompanying source code for this article.) First, create a new Visual C# Windows Form application using Visual Studio 2005 (Note: If you use Visual Studio 2003, the code will work the same). Once you've created the project, add the app.config created through the Enterprise Library Configuration Tool to the root of the project. Also add the cacheConfiguration.config file to the bin/debug directory that is under the project root directory. Once the project is created, add a reference to the caching block namespaces as follows:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

Add a private variable of type CacheManager object as shown below:

private CacheManager cacheManager;

The cacheManager variable must be able to reference a valid CacheManager object. For that, add the following line of code to the form constructor:

   // Use the default cache manager for the cache operations
   cacheManager = CacheFactory.GetCacheManager();

The above code returns a reference to the CacheManager object through the GetCacheManager() method of the CacheFactory class. The parameter-less GetCacheManager() method returns a reference to the default cache manager, which in this case is in-memory. You can also return a reference to a specific predefined cache manager by specifying the name of the cache manager class. This is defined in the following cacheConfiguration.config file:
<cacheManagers>
  <cacheManager name="Default Cache Manager" 
    expirationPollFrequencyInSeconds="60" 
    maximumElementsInCacheBeforeScavenging="1000" 
    numberToRemoveWhenScavenging="10">
    <cacheStorage xsi:type="CustomCacheStorageData" name="Null Storage"    
      type="Microsoft.Practices.EnterpriseLibrary.Caching.
      BackingStoreImplementations.NullBackingStore, 
      Microsoft.Practices.EnterpriseLibrary.Caching">
      <extensions />
    </cacheStorage>
  </cacheManager>
 --------
 --------
 --------
</cacheManagers>

Now open up the Windows Form and add a command button named btnAddItem. Modify its Click event to look as follows:

 private void btnAddItem_Click(object sender, EventArgs e)
 {            
    NameValuePair obj = new NameValuePair();
    obj.Name ="First Item";
    obj.Value ="1";
    cacheManager.Add("1", obj,CacheItemPriority.High, new 
      CustomCacheRefreshAction(),
      new AbsoluteTime(DateTime.Now.AddHours(24)));
    MessageBox.Show("Item added successfully");
 }

The NameValuePair class in the above example is defined as follows:

[Serializable]
public class NameValuePair
{
   string _name;
   string _value;
  
   public string Name
   {
      get
      {
         return _name;
      }
      set
      {
         _name = value;
      }
    }
    public string Value
    {
       get
     {
        return _value;
     }
     set
     {
        _value = value;
     }
   }
}

In the Click event of the btnAddItem button, start by creating an instance of the NameValuePair object and setting its values appropriately:

    NameValuePair obj = new NameValuePair();
    obj.Name ="First Item";
    obj.Value ="1";

After that, add the NameValuePair object to the cache by invoking the Add() method of the CacheManager object:

    cacheManager.Add("1", obj,CacheItemPriority.High, new 
      CustomCacheRefreshAction(),
      new AbsoluteTime(DateTime.Now.AddHours(24)));

The Add() method accepts the following parameters:

  • A key that uniquely identifies the stored item in the cache
  • Enumeration that specifies the priority of the item stored in the cache
  • A custom class that implements the ICacheItemRefreshAction interface (The Refresh() method of the ICacheItemRefreshAction interface allows you to execute custom code whenever the item has been removed from the cache.)
  • Time at which the cached item needs to expire

For the purposes of this example, the CustomCacheRefreshAction class contains just the declaration of the Refresh() method:

[Serializable]
public class CustomCacheRefreshAction : ICacheItemRefreshAction
{
  public void Refresh(string key, object expiredValue, 
    CacheItemRemovedReason removalReason)
  {
     // Item has been removed from cache. Perform desired actions here, 
     //based upon the removal reason (e.g. refresh the cache with the item).
  }
}

Finally, display a message box indicating that the item is added successfully:

    MessageBox.Show("Item added successfully");

If you run the application and click the command button, you should see a message box that says "Item added successfully."

Retrieving an Item from the Cache

Now that you have successfully added an item to the cache, look at the code required to retrieve that item from the cache. Add a command button named btnRetrieve to the form and modify its click event to look as follows:
private void btnRetrieve_Click(object sender, EventArgs e)
{
   NameValuePair obj = (NameValuePair)cacheManager.GetData("1");
   if (obj != null)
      MessageBox.Show("Name: " + obj.Name + ", Value = " + obj.Value);
   else
      MessageBox.Show("Item not found in the cache");
}

Retrieving an item from the cache requires the use of the GetData() method of the CacheManager object. To this method, supply the key of the cached item. It returns an object of type object, which you can then type cast into a NameValuePair object. Then you simply display the values contained in the NameValuePair object through the message box, if the object returned by the CacheManager is a valid object. If you run the application, you can get output similar to Figure 2.

Figure 2. Output from Application Retrieving an Item from Cache

Removing an Item from the Cache

Once you have an item in the cache, you might want to remove it. As with retrieving an item from the cache, removing an item also requires you to supply the key of the cached item as an argument. The CacheManager object exposes the Remove() method for this purpose. Here is the code required to remove an item from the cache:
private void btnRemove_Click(object sender, EventArgs e)
{
   cacheManager.Remove("1");
   MessageBox.Show("Item removed from the cache");
}

To the Remove() method, you supply the key of the cached item.

Flushing the Cache

The last use case in this example is flushing out the cache. You might need to flush out the cache before you shut your application down. To this end, the CacheManager exposes a method named Flush() that you can call to flush the cache:
private void btnFlush_Click(object sender, EventArgs e)
{
   cacheManager.Flush();
   MessageBox.Show("Item flushed from the cache");
} 

As expected, the Flush() method does not take in any parameters. It simply clears all the items from the cache.

Caching: The Performance Booster

Caching has a dramatic impact on the performance of database-driven .NET applications. You can use caching to store data that an application consumes frequently; you can also use it to store data that is expensive to create, obtain, or transform. The EntLib caching block enables you to implement caching functionality in your applications easily. By using the caching block, you can also eliminate errors, bugs, and typos in your application by reusing proven and tested code.

Download the Code

To download the accompanying source code for the examples, click here.

About the Author

Thiru Thangarathinam has many years of experience in architecting, designing, developing, and implementing applications using object-oriented application development methodologies. His certifications include MCAD for .NET, MCSD, and MCP. Thiru is an expert in ASP.NET, .NET Framework, Visual C# .NET, Visual Basic .NET, ADO.NET, XML Web services, and .NET remoting. Thiru also has authored numerous books and articles. Contact him at thiruthangarathinam@yahoo.com.





Page 2 of 2



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel