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.

Ff649804.ca2a8c42-30e7-4c2d-8d66-36a669caee6e(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.
  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).
  4. Load the entire set of data from the XML file into the cache.

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.
  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).
  4. Add the following code that will execute when a request is made to retrieve a Product.

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.