Introduction to the Caching Application Block

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The Enterprise Library Caching Application Block lets developers incorporate a local cache in their applications. It supports both an in-memory cache and, optionally, a backing store that can either be the database store or isolated storage. The application block can be used without modification; it provides all the needed functionality to retrieve, add, and remove cached data. Configurable expiration and scavenging policies are also part of the application block.

When building enterprise-scale distributed applications, architects and developers are faced with many challenges. Caching can help them to overcome some of these challenges, including the following:

  • Performance. Caching improves application performance by storing relevant data as close as possible to the data consumer. This avoids repetitive data creation, processing, and transportation.
  • Scalability. Storing information in a cache helps save resources and increases scalability as the demands on the application increase.
  • Availability. By storing data in a local cache, the application may be able to survive system failures such as network latency, Web service problems, and hardware failures.

Common Scenarios

The Caching Application Block is suitable if you encounter any of the following situations:

  • You must repeatedly access static data or data that rarely changes.
  • Data access is expensive in terms of creation, access, or transportation.
  • Data must always be available, even when the source, such as a server, is not available.

You can use the Caching Application Block with any of the following application types:

  • Windows Forms
  • Console application
  • Windows service
  • COM+ server
  • ASP.NET Web application or Web service if you need features not included in the ASP.NET cache

You should deploy the Caching Application Block within a single application domain. Each application domain can have one or multiple caches, either with or without backing stores. Caches cannot be shared among different application domains.

The Caching Application Block is optimized for performance and is both thread safe and exception safe. You can extend it to include your own expiration policies and your own backing store.

Example Application Code

The following code shows how to add an item to a cache and retrieve an item from the cache. It creates an object of type Product and then adds it to the cache, together with a scavenging priority of 2, an instruction not to refresh the item if it expires, and an expiration date of 5 minutes from the last time the item was accessed.

Note

The code does not include the Product class definition.

CacheManager productsCache = CacheFactory.GetCacheManager();

string id = "ProductOneId";
string name = "ProductXYName";
int price = 50;

Product product = new Product(id, name, price);

productsCache.Add(product.ProductID, product, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(5)));

// Retrieve the item.
product = (Product) productsCache.GetData(id);
Dim productsCache As CacheManager = CacheFactory.GetCacheManager()

Dim id As String = "ProductOneId"
Dim name As String = "ProductOneName"
Dim price As Integer = 50

Dim newProduct As Product = New Product(id, name, price)

productsCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, Nothing, New SlidingTime(TimeSpan.FromMinutes(5)))

' Retrieve the item.
product = DirectCast(productsCache.GetData(id), Product)

Audience Requirements

This guide is intended for software architects and software developers. To get the most benefit from this guide, you should understand the following technologies:

  • Microsoft Visual Studio 2005 development system
  • Microsoft .NET Framework 2.0

Highlights of the Enterprise Library Caching Application Block

The Enterprise Library Caching Application Block includes the following features:

  • You can use a graphical tool, named the Enterprise Library Configuration Console, for managing configuration settings.
  • You can configure a persistent storage location, using either isolated storage or the Enterprise Library Data Access Application Block, whose state is synchronized with the in-memory cache.
  • You can extend the application block by creating custom expiration policies and storage locations.
  • You receive assurance that the application block performs in a thread-safe manner.

System Requirements

The requirements to run the Caching Application Block are the following:

  • Microsoft Windows XP Professional, Windows Server 2003, or Windows Vista operating system

  • Microsoft .NET Framework 2.0

  • Microsoft Visual Studio 2005 development system (any of the following editions):

    Microsoft Visual Studio 2005 Standard Edition

    Microsoft Visual Studio 2005 Professional Edition

    Microsoft Visual Studio 2005 Team Edition for Software Developers

    Microsoft Visual Studio 2005 Team Edition for Software Testers

    Microsoft Visual Studio 2005 Team Edition for Software Architects

    Microsoft Visual Studio 2005 Team Suite

Caching Application Block Dependencies

The Caching Application Block depends on other code included in the Enterprise Library:

  • Core library functionality. The Enterprise Library Core provides services such as instrumentation and configuration; it is a shared dependency of all Enterprise Library application blocks. The core library functionality is contained in the assembly Microsoft.Practices.EnterpriseLibrary.Common.dll.
  • The ObjectBuilder subsystem. The ObjectBuilder subsystem performs all the repetitive and necessary tasks for creating and disposing object instances, while still providing a high level of flexibility. Enterprise Library uses the ObjectBuilder subsystem for tasks such as injecting configuration into block classes and connecting instrumentation classes to application blocks. The ObjectBuilder subsystem is contained in the assembly Microsoft.Practices.ObjectBuilder.dll.
  • The Data Access Application Block. You need this application block if you are going to use a database as a backing store.
  • The Cryptography Application Block. You need this application block if you are going to encrypt data in the cache.

The recommended way to modify the configuration settings for the Caching Application Block is to use the Enterprise Library Configuration Console.

Caching Application Block Documentation

In addition to the introductory material, the documentation contains the following topics:

  • Developing Applications Using the Caching Application Block. This topic first explains how to configure the Caching Application Block and add it to your application. It then explains how to select a backing store. Next, the Key Scenarios section demonstrates how to use the application block to perform typical caching operations.
  • Design of the Caching Application Block. This topic explains the decisions that went into designing the application block and the rationale behind those decisions.
  • Extending and Modifying the Caching Application Block. This topic explains how to extend the application block by adding your own backing store and your own expiration policies. It also explains how to modify it by changing the source code.
  • Deployment and Operations. This topic explains how to deploy and update the application block assemblies.
  • QuickStarts. This topic explains how to install and configure the QuickStart application and contains a series of walkthroughs that demonstrate how to incorporate common caching operations into an application.

More Information

For related information, see the following patterns & practices guides:

**Application Architecture for .NET: Designing Applications and Services

**Caching Architecture Guide for .NET Framework Applications

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.