Code to Use Caching with a Catalog

This example is based on the Retail Solution Site, but has been substantially simplified for clarity. See the global_cache_lib.asp page for details about all the cache initialization in the site. See the std_cache_lib.asp page for methods that encapsulate writing to and retrieving data from the caches on the site.

A cache can be either a Dictionary or LRUCache object. The following steps demonstrate the use of an LRUCache object to store HTML fragments. Using a Dictionary object is similar, but not identical; see the source code mentioned above for details about using Dictionary objects with the CacheManager object.

You can use caches to store objects, but this approach has a significant limitation. The cache will become invalid as soon as the object goes out of scope. For example, you cannot cache any type of catalog object or recordset between page requests. The Retail site does not cache objects.

The following code demonstrates:

  • How to set up the CacheManager object in the Application_OnStart event of the Global.asa file
  • How to set up the LRUCache object in the Global.asa file.
  • How to access the cache within Active Server Pages (ASP) pages by using the CacheManager object.
  • How to store and retrieve HTML fragments on an ASP page by using the LRUCache object.
  1. Create the CacheManager object in the Global.asa file.

    Dim oCacheManager
    
    Set oCacheManager = Server.CreateObject("Commerce.CacheManager") 
    
    ' SMachineBaseUrl has been set during initialization.
    ' It is the fully qualified URL of the application,
    ' not the localhost URL
    oCacheManager.AppUrl = sMachineBaseUrl 
    
    'Make the CacheManager object available throughout the application.
    Set Application("MSCSCacheManager") = oCacheManager
    
  2. Create and register the Cache object. For more information about the configuration settings, see LRUCache Object.

    ' Create a configuration dictionary for the cache.
    Set dictProductPageCacheConfig = GetDictionary() 
    ' retrieve the connection string 
    ' from the global configuration dictionary
    dictProductPageCacheConfig("ConnectionString") = _ 
        dictConfig.s_TransactionConfigConnectionString dictProductPageCacheConfig("CacheSize") = _
        10000 
    dictProductPageCacheConfig("TableName") = _ 
        "CatalogCache_Virtual_Directory"
    dictProductPageCacheConfig("CacheName") = _
        "CatalogCache" 
    dictProductPageCacheConfig("AppUrl") = _
        sMachineBaseURL 
    
    ' Configure CacheManager For ProductList Fragment Caching oCacheManager.RefreshInterval("ProductPageCache") = _
        0 
    oCacheManager.RetryInterval("ProductPageCache") = _
        5 * 60 
    oCacheManager.CacheObjectProgId("ProductPageCache") = _
        "Commerce.LRUCache" 
    oCacheManager.LoaderProgId("ProductPageCache") = _
        "Commerce.LRUCacheFlush" 
    Set oCacheManager.LoaderConfig("ProductPageCache") = _
        dictProductPageCacheConfig 
    
    ' Call GetCache() to register the cache with the Business Desk.
    Call oCacheManager.GetCache("ProductPageCache") 
    
  3. Obtain the cache from the CacheManager object on the ASP page.

    Dim oLRUCache
    Set oLRUCache = MSCSCacheManager.GetCache("ProductPageCache") 
    
  4. Store and retrieve HTML fragments on an ASP page by using the LRUCache object. The Retail site attempts to retrieve the fragment before rendering new HTML. If no fragment is available from the cache, the site renders new HTML and caches it.

    ' Create a key that uniquely defines the fragment, in this case a product.
    Dim sCacheKey
    
    ' sCatalogName & sCategoryName & sProductID & sVariantID 
    ' are existing strings on the Product page.
    sCacheKey = sCatalogName & sCategoryName & sProductID & sVariantID 
    
    ' sLanguage is the language of the page.
    htmPageContent = oLRUCache.Lookup(sCacheKey & sLanguage)
    
    ' Test if a fragment was retreived from the cache.
    If IsNull(htmPageContent) Then 
        ' Do a lot of work that puts content in htmPageContent.
        Call oLRUCache.Insert(sCacheKey & sLanguage, htmPageContent)
    End If 
    

Copyright © 2005 Microsoft Corporation.
All rights reserved.