Understanding Caching in Commerce Server

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

Effective caching is important for getting the maximum performance and scalability from your e-commerce Web applications. Commerce Server 2007 and ASP.NET both provide a range of caching capabilities that you can take advantage of in your Commerce Server development projects.

Caching in the Commerce Server Systems

Many Commerce Server systems use forms of caching:

  • The Catalog System includes a built-in cache that caches the catalog item data returned by various Catalog System properties and methods. Cache tuning is achieved by modifying the attributes in the cache element in the catalog element in the Web.config file for your application. Internally, the Catalog System cache uses the ASP.NET caching facility. This allows for memory to be managed effectively together with ASP.NET page and fragment caching, and with any other custom caching that is being performed in your Web applications. For more information about how to enable Catalog System caching capabilities, see Caching in the Catalog System.

  • The Marketing System caches the complete set of active advertisements and discounts when these caches are configured in the Web.config file for the application. Public promotion codes associated with discounts are also loaded into the discounts cache. This allows for very low latency coupon support. The advertising and discount caches are configured in the caches element in the Web.config file.

    The Marketing System uses targeting expressions to allow for a boundless array of advertisement and discount targeting scenarios. The expression evaluation engine in Commerce Server includes a built-in cache that contains expressions that are being used for marketing campaign items. Expression evaluation can be done in a cached context so that multiple expressions can be evaluated efficiently for a distinct set of profiles, such as the same user and targeting context. For more information about this technique, see the EvaluateInContext method of the ExpressionEvaluator object. The Expression Evaluator caches are not configurable.

  • The Orders System includes caches for shipping methods, payment methods, and for other orders configuration datasets such as region codes and status codes. These caches are configured in the caches element in the Web.config file.

  • The Profiles System includes an internal cache of profile objects. Repeated profile object retrievals for the same profile in the same ASP.NET-connected application instance will cause the profile to be returned with very low latency from the cache without any disk I/O or SQL Server access. Caching in the Profile System cannot be disabled. However, the cache size and other cache tuning parameters can be set by modifying the configuration in the profilesWebService element in the Web.config file.

Refreshing the Cache

You can specify how frequently the system refreshes the caches by using the settings of the caches element in the Web.config file. The retryInterval attribute of this element specifies the number of seconds the cache manager will wait before it tries to refill the cache if an error is encountered. The refreshInterval attribute specifies the number of seconds until the cache expires and the loader is invoked to refill the cache.

In addition to these time intervals, you can use code to force the cache to refresh. For example, you might want to refresh the caches if a specific error occurs, or you may want to clear the caches and obtain the latest changes.

The URL of the cache to refresh is controlled by a registry setting. If the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Commerce Server 2007\CacheRefreshMode\BuildCacheRefreshURLWithHostName is present, the URL is constructed by using the host name; otherwise the URL is constructed by using the name of the server.

Procedures

To refresh all the configured caches

  1. Use the Caches property on the CommerceContext object to retrieve a collection of all caches in the Commerce Server system.

  2. For each cache, use the Refresh method on the CommerceCache object to refresh all caches in the Commerce Server systems.

To refresh the cache of a single system

Example

The following code samples show you how to refresh the caches in the Commerce Server system. The first method uses the Caches property on the CommerceContext object to retrieve a collection of all caches in the Commerce Server system. The example then calls the Refresh method on the CommerceCache object to refresh each cache in the Commerce Server system.

The remaining examples show you how to refresh cache of a single system. These examples use the Refresh method to refresh the specified cache.

 [C#]
public static void RefreshAllCaches()
{
    CommerceCacheCollection caches = CommerceContext.Current.Caches;
    foreach (CommerceCache cache in caches)
    {
        cache.Refresh();
    }
}

public static void RefreshShippingMethodCache()
{
    if (CommerceContext.Current.Caches["ShippingManagerCache"] != null)
        CommerceContext.Current.Caches["ShippingManagerCache"].Refresh();
}

public static void RefreshPaymentMethodCache()
{
    if (CommerceContext.Current.Caches["PaymentMethodCache"] != null)
        CommerceContext.Current.Caches["PaymentMethodCache"].Refresh();
}

public static void RefreshOrdersConfigurationCache()
{
    if (CommerceContext.Current.Caches["OrdersConfigurationCache"] != null)
        CommerceContext.Current.Caches["OrdersConfigurationCache"].Refresh();
}

ASP.NET Application-Level Caching

You can configure page output caching by setting the OutputCache directive in an .aspx page. This will cause the complete rendered page to be cached for a configurable time period.

For example: <%@ OutputCache Duration="60" VaryByParam="None" %>

Fragment output caching, in which only a rendered piece of a dynamic ASP.NET page is cached, can be done in a similar manner as page output caching by using ASP.NET user controls (.ascx files). This kind of caching is appropriate to use, for example, when you want to display a dynamic advertisement targeted for the current site user when also outputting a cached rendering of a product from the catalog system.

Note

Previous versions of Commerce Server used the LRUCache object to enable fragment caching within ASP pages. This object is provided in Commerce Server 2007 for backward compatibility with existing Commerce Server Web applications. For new development, you should use one of the caching facilities described here or use the ASP.NET cache API directly, because it provides rich tuning and auto-sizing features.

For information about ASP.NET caching that include caching whole ASP.NET pages, sections of ASP.NET pages, and the reference information for the System.Web.Caching namespace, see:

See Also

Other Resources

caches Element

Development Concepts