http://www.developer.com/net/net/article.php/3563726/Implement-Caching-to-Give-Your-NET-Applications-a-Performance-Boost.htm
The most dramatic way to improve the performance of a database-driven application is through caching. Retrieving data from a database is one of the slowest operations that a Web application performs. If it caches the database data in memory, however, then it avoids accessing the database with every requestwhich dramatically increases its performance.
To implement caching, you must create common plumbing code that provides the basic functionality. Instead of creating this code in every project, wouldn't it be great to encapsulate this common plumbing code in a reusable block and reuse it in different projects? Well, that is exactly what the Enterprise Library (EntLib) caching block provides. This article introduces the EntLib caching block and demonstrates how to leverage it from a .NET application.
By default, the caching block provides the following three types of caching mechanisms:
The caching block is suitable for any of the following situations:
You can use the caching block with any of the following application types:
The CacheManager class is the primary interface between the application and the rest of the caching block. It handles operations related to caching by creating a Cache object, which is an in-memory representation of the backing store. You also can configure this class to store data either only in memory or both in memory and in persistent storage.
To create an instance of a CacheManager object, you need to use the CacheFactory class, which in turn uses the CacheManagerFactory class. The CacheManagerFactory class creates all the internal classes needed to implement a CacheManager object.
Figure 1. Configuration Settings for Examples Now that you have a general understanding of the EntLib caching block, let's look at some examples.
Add a private variable of type CacheManager object as shown below: The cacheManager variable must be able to reference a valid CacheManager object. For that, add the following line of code to the form constructor: 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: Now open up the Windows Form and add a command button named btnAddItem. Modify its Click event to look as follows: The NameValuePair class in the above example is defined as follows: In the Click event of the btnAddItem button, start by creating an instance of the NameValuePair object and setting its values appropriately: After that, add the NameValuePair object to the cache by invoking the Add() method of the CacheManager object: The Add() method accepts the following parameters:
For the purposes of this example, the CustomCacheRefreshAction class contains just the declaration of the Refresh() method: Finally, display a message box indicating that the item is 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 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 To the Remove() method, you supply the key of the cached item.
As expected, the Flush() method does not take in any parameters. It simply clears all the items from the cache.
To download the accompanying source code for the examples, click here.
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.
Implement Caching to Give Your .NET Applications a Performance Boost
November 14, 2005
How the EntLib Caching Block Works
The EntLib caching block allows you to incorporate a local cache in your applications. It is ready to use without any modification; it provides all the functionality you need to retrieve, add, and remove cached data. The caching block also enables you to configure expiration and scavenging policies for the cached items.
Installation
Before using the EntLib caching block, take the following steps to get it installed and configured in your local machine:
Now you're ready to begin. Start by creating the configuration file through the configuration utility.
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
Creating Configuration Files
The Enterprise Library Configuration Tool helps create the necessary configuration files so that the caching block can retrieve the configuration settings from a set of configuration files. To create the configuration file, bring up the Enterprise Library Configuration Tool and select File->New Application from the menu. Right click on the newly created application (named Application1) and select New->Caching Application Block from the context menu and specify the configuration settings. Figure 1 shows the configuration settings that the examples in this article use.

Click here for a larger image. 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;
private CacheManager cacheManager;
// Use the default cache manager for the cache operations
cacheManager = CacheFactory.GetCacheManager();
<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>
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");
}
[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;
}
}
}
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)));
[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).
}
}
MessageBox.Show("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");
}

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");
}
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");
}
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
About the Author