Share via


Walkthrough: Loading the Cache

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.

This section of the QuickStart demonstrates how to load the cache proactively and how to load the cache reactively.

Solution Overview

Figure 1 illustrates how the QuickStart retrieves data from the cache.

Ff649311.d0944ae8-d20e-47f2-8760-b2ede264c473(en-us,PandP.10).png

Figure 1
Solution elements for loading the cache

Figure 1 illustrates the following points:

  • Sample product data is contained within the XML file, CachingQuickStartData.xml.
  • The DataProvider object reads product data from the XML file.
  • The ProductData object uses the DataProvider class to obtain product data that it then adds to a cache.
  • The QuickStartForm object retrieves product information by calling the ProductData object. When queried for product information, the ProductData object first looks in the cache and, if found, returns the information. For items that are not in the cache, the ProductData object uses the DataProvider object to retrieve the data from the file, and then add it to the cache.

Loading the Cache Proactively

You can cache data proactively by retrieving all the required state for an application or a process, typically when the application or process starts, and caching it for the lifetime of the application or process.

To load the cache proactively

  1. Configure the cache, defining a CacheManager object with the name Loading Scenario Cache Manager. For the necessary steps, see "QuickStart Configuration" in Caching QuickStart.

  2. Declare a member variable of type ICacheManager to store the CacheManager object in the ProductData class.

    private ICacheManager cache;
    
    'Usage
    Private cache As ICacheManager
    
  3. Create the CacheManager object by adding the following code. The factory creates the CacheManager object using the name in the configuration file(when not using the Unity Integration approach).

    cache = CacheFactory.GetCacheManager("Loading Scenario Cache Manager");
    
    'Usage
    cache = CacheFactory.GetCacheManager("Loading Scenario Cache Manager")
    
  4. Load the entire set of data from the XML file into the cache.

    List<Product> list = this.dataProvider.GetProductList();
    
    for (int i = 0; i < list.Count; i++)
    {
      Product product = list[i];
      cache.Add(product.ProductID, product);
    }
    
    'Usage
    Dim list As List(Of Product) = Me.dataProvider.GetProductList()
    
    Dim i As Integer
    For i = 0 To list.Count - 1
      Dim newProduct As Product = list(i)
      cache.Add(newProduct.ProductID, newProduct)
    Next
    

Loading the Cache Reactively

You can reactively cache data by retrieving data only when it is requested by the application, and caching it for future requests.

To load the cache reactively

  1. Configure the cache, defining a CacheManager with the name Loading Scenario Cache Manager. For the necessary steps, see "QuickStart Configuration" in Caching QuickStart.

  2. Declare a member variable of type ICacheManager to store the CacheManager in the ProductData class.

    private ICacheManager cache;
    
    'Usage
    Private cache As ICacheManager
    
  3. Create the CacheManager by adding the following code. The factory creates the CacheManager object using the name in the configuration file (when not using the Unity Integration approach).

    cache = CacheFactory.GetCacheManager("Loading Scenario Cache Manager");
    
    'Usage
    cache = CacheFactory.GetCacheManager("Loading Scenario Cache Manager")
    
  4. Add the following code that will execute when a request is made to retrieve a Product.

    Product product = (Product)cache.GetData(productID);
    
    // Does our cache already have the requested object?
    if (product == null)
    {
      // Requested object is not cached; therefore, retrieve it from
      // the data provider and cache it for more requests.
      product = this.dataProvider.GetProductByID(productID);
    
      if (product != null)
      {
        cache.Add(productID, product);
      }
    }
    
    return product;
    
    'Usage
    Dim requestedProduct As Product = DirectCast(cache.GetData(productID), Product)
    
    ' Does our cache already have the requested object?
    If (requestedProduct Is Nothing) Then
    
      ' Requested object is not cached; therefore retrieve it from
      ' data provider and cache it for further requests.
      requestedProduct = Me.dataProvider.GetProductByID(productID)
    
      If (Not requestedProduct Is Nothing) Then
        cache.Add(productID, requestedProduct)
      End If
    
    End If
    
    Return requestedProduct
    

Comparing the Cache to the Master Data Source

To see how data in the cache can differ from what is in the master data source, click the Edit Master Data button and change some of the values in the XML file that acts as the master data source. If an item was already in the cache before the master data changed and you retrieve it from the cache after changing the data, the data retrieved from the cache will not match the data in the master data source.