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.

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.

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;
    
    
  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");
    
    
  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);
    }
    
    

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;
    
    
  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");
    
    
  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;
    
    

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.



Show: