Code to Use Caching with a Catalog
The following code demonstrates both the CacheManager object setup code required in the Application_OnStart event of the Global.asa file, and the CacheManager object access code required within Active Server Pages (ASP) using the CacheManager object. The cache can be either a Dictionary or LRUCache object. The following steps demonstrate the use of an LRUCache object and stores recordsets. You can use a similar cache to store HTML.
Create the CacheManager object in the global.asa file.
Dim oCacheManager Set oCacheManager = Server.CreateObject("Commerce.Cachemanager") oCacheManager.AppUrl = MY_APPLICATION_URL Set Application("MSCSCacheManager") = oCacheManager ' Create a configuration dictionary for the cache. dim dictProductCategoryCacheConfig set dictProductCategoryCacheConfig = GetDictionary() dictProductCategoryCacheConfig("ConnectionString") = _ TRANSACTION_CONFIG_CONNECTION_STRING dictProductCategoryCacheConfig("CacheSize") = 10000 dictProductCategoryCacheConfig("TableName") = _ "CatalogCache_Virtual_Directory" dictProductCategoryCacheConfig("Cachename") = PRODUCT_CATEGORY_CACHE dictProductCategoryCacheConfig("AppURL") = MY_APPLICATION_URL ' Create and initialize the cache to use LRUCache objects. oCacheManager.RefreshInterval(PRODUCT_CATEGORY_CACHE) = 0 oCacheManager.RetryInterval(PRODUCT_CATEGORY_CACHE) = 5 * 60 oCachemanager.CacheObjectProgId(PRODUCT_CATEGORY_CACHE) = _ "Commerce.LRUCache" oCacheManager.LoaderProgID(PRODUCT_CATEGORY_CACHE) = _ "Commerce.LRUCacheFlush" Set oCacheManager.LoaderConfig(PRODUCT_CATEGORY_CACHE) = _ dictProductCategoryCacheConfig ' LRUCacheFlush is a cache loader component supplied ' to work specifically with LRUCaches.
Get the cache in the ASP page. The CacheManager object will return a cache by name. If the requested cache does not exist, the CacheManager object will automatically create an empty one.
' Get a local reference to the CacheManager object on the ASP page: Dim oCache Dim g_MSCSCacheManager Set g_MSCSCacheManager = Application("MSCSCacheManager") ' Get the cache from the CacheManager object: Set oCache = g_MSCSCacheManager.getCache(PRODUCT_CATEGORY_CACHE)
Perform a lookup. This example is part of a function named GetCategoriesRS that returns the child categories if given the name of the parent category. The name of the parent category, contained in the variable
parentCategory
, serves as the cache key.Dim rsChildCategories ' First, try to retrieve the child ' categories recordset from the cache. Set rsChildCategories = oCache.Lookup(parentCategory) ' Test whether the cache returned the requested recordset. If Not IsNull(rsChildCategories) Then ' It did, so return the recordset. Set GetCategoriesRS = rsChildCategories Else ' It did not, so get it from the catalog. Set rsChildCategories = _ oCatalog.GetCategory(parentCategory).ChildCategories ' Put the recordset in the cache for future use. Set oCache.Insert(parentCategory, rsChildCategories) ' Return the child categories recordset. Set GetCategoriesRS = rsChildCategories End If