Retrieving Items from the Cache

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

Data stored in the cache must be retrieved so that it can be displayed or processed. For example, in a retail application, you may want to display a list of products from a catalog.

Typical Goals

In this scenario, you want to retrieve a particular item from the cache.

Solution

Use the GetData method provided by the CacheManager class; it returns the value associated with a specific key.

Using the GetData Method

The following code shows how to use the GetData method. Add the code to the method responding to the request to retrieve an item from the cache.

C# Copy Code
// Read the item from the cache. If the item is not found
// in the cache, the return value will be null.
public Product GetProduct(ICacheManager cache, string key)
{
  return (Product)cache.GetData(key);
}
Visual Basic Copy Code
' Read the item from the cache. If the item is not found
' in the cache, the return value will be null.
Public Function GetProduct(ByVal cache As ICacheManager, ByVal key As String) As Product
  Return DirectCast(Cache.GetData(key), Product)
End Function