Partager via


What Does the Caching Application Block Do?

The Caching Application Block provides an in-memory cache that your application can manipulate through a simple API to store and retrieve items, and to obtain information about the stored items. In addition, you can configure a persistent or custom backing store for your cache, and—if required—encrypt the information stored there. You can configure more than one cache for your application, and specify a partition for each one so that data from multiple caches can be stored in separate containers within the same backing store location, such as a database. The following schematic shows the main elements of the Caching Application Block.

Ff664752.0f0609a7-acad-4f63-b026-2630be96c1be(en-us,PandP.50).png

At application startup, the block loads the in-memory cache from the backing store (if configured). Alternatively, you can also load the cache yourself if you want to implement a delayed loading pattern. As the application runs, the block checks the expiration of cached items and removes them from the cache. Expiry can be configured based on sliding or absolute time values; or through dependencies on files, other cached items, or external resources. The block also manages the cache in conjunction with memory availability, based on cached item priorities.

Note

For information about the types of expiration you can use, and the default expiration for new items, see Design of the Expiration Process.

As application code interacts with the Cache Manager, it updates the in-memory cache and—if a backing store is configured—updates the backing store. You can configure an encryption provider for the backing store, which is implemented by the Cryptography Application Block, to encrypt the items that are cached in the backing store (note that the block does not encrypt items stored in the in-memory cache).

The block includes providers that store data in a database or in Isolated Storage on the local machine. It does not provide a distributed caching mechanism. Additional providers, including providers that support distributed caching, may be available from third parties and the Enterprise Library community Web site. For more information, see the CodePlex Community and Enterprise Library Contributions Web sites.

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 with the product ID as the cache key, a scavenging priority of Normal, 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. The code does not include the Product class definition, and assumes that you resolve the class shown below through the Enterprise Library container to populate the dependency on the default configured CacheManager instance.

public void AddProductToCache(ICacheManager productsCache)
{
  string id = "ProductOneId";
  string name = "ProductOneName";
  int price = 50;

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

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

  // Retrieve the item.
  newProduct = (Product) productsCache.GetData(id);
}
'Usage
Public Sub AddProductToCache(ByVal productsCache As ICacheManager)

  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.
  newProduct = DirectCast(productsCache.GetData(id), Product)

End Sub

For information about resolving Enterprise Library objects in your applications, see Creating and Referencing Enterprise Library Objects.