Partilhar via


Retrieving Items from the Cache

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.

// 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);
}
'Usage
' 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