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, it avoids accessing the database with every request—which 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.
How the EntLib Caching Block Works
The EntLib caching block enables 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.
By default, the caching block provides the following three types of caching mechanisms:
- In memory (Null): The in-memory cache stores the cached items in memory. It’s non-permanent, but fast.
- IsolatedStorage: This cache store is permanent but slightly slower than in-memory. For example, it could be an XML file or an external file.
- Database: As with IsolatedStorage, the database option also is slower. It requires the use of the EntLib data access block as well.
The caching block is suitable for any of the following situations:
- Static data needs to be repeatedly accessed
- Creation, processing, and transportation of data is expensive
- High availability is a major factor
You can use the caching block with any of the following application types:
- Windows Forms
- Windows Service
- Enterprise Services
- ASP.NET Web application or Web service if you need features that are not included in the ASP.NET cache
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.
Before using the EntLib caching block, take the following steps to get it installed and configured in your local machine:
- Download the EntLib.
- Navigate the Start menu to All Programs->Microsoft patterns and practices->Enterprise Library->Build Enterprise Library. This will build all the EntLib blocks including the caching block.
- Once the assembly has been built, you can add references to the caching block assembly and the required dependant assemblies (Microsoft.Practices.EnterpriseLibrary.Caching.dll, Microsoft.Practices.EnterpriseLibrary.Common.dll, and Microsoft.Practices.EnterpriseLibrary.Configuration.dll) from <DriveName>:\Program Files\Microsoft Enterprise Library June 2005\bin.
- Create the necessary configuration files using the Enterprise Library Configuration Tool.
- Once the assemblies are referenced, import the required namespaces. To leverage the EntLib caching block, add the following imports statements to your code:
Note: A reference to the configuration block assembly is required because the caching block has a dependency on the configuration for reading configuration settings from the configuration file.
using Microsoft.Practices.EnterpriseLibrary.Caching; using Microsoft.Practices.EnterpriseLibrary.Caching. Expirations;
Now, you’re ready to begin. Start by creating the configuration file through the configuration utility.
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.
Figure 1: Configuration Settings for Examples
When you save the settings shown in Figure 1, the tool will create two files: one with the name that you entered in the File Save As prompt (specify the name as app.config file for the purposes of this example) and the other one with the name cachingConfiguration.config. The cachingConfiguration.config file is the one that contains the actual cache-related configuration settings.
Now that you have a general understanding of the EntLib caching block, let’s look at some examples.