Export (0) Print
Expand All

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.

Ff648256.CA-QS-01-LoadingCache(en-us,PandP.10).gif

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 to retrieve all the required state for an application or a process, typically when the application or process starts, and cache 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 for the CacheManager object in the ProductData class.
    private CacheManager cache;
    
    
    Private cache As CacheManager
    
    
  3. Create the CacheManager object by adding the following code. The factory creates the CacheManager object using the name in the configuration file.
    cache = CacheFactory.GetCacheManager("Loading Scenario Cache Manager");
    
    
    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);
    }
    
    
    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 to retrieve data as it is requested by the application and cache 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 for the CacheManager in the ProductData class.
    private CacheManager cache;
    
    
    Private cache As CacheManager
    
    

  3. Create the CacheManager by adding the following code. The factory creates the CacheManager object using the name in the configuration file.
    cache = CacheFactory.GetCacheManager("Loading Scenario Cache Manager");
    
    
    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;
    
    
    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.

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.
Show:
© 2014 Microsoft