Use Custom Caches

To store, retrieve, or manage a cache entry, use the B2C Commerce script API classes dw.system.CacheMgr and dw.system.Cache.

The dw.system.CacheMgr.getCache(cacheID) method returns caches that are already defined.

var cache = CacheMgr.getCache( 'SiteConfigurations' );

Entries within the cache are identified by a key. The dw.system.Cache.get(key) method returns the object associated with the key if an entry exists. To specify a scope, such as site or catalog, include a scope reference in the key construction.

cache.get( Site.current.ID + "config", function loadSiteConfiguration() {return loadCfg( Site.current );} );

A best practice is to populate a cache using dw.system.Cache.get(key, loader), which returns the value associated with the key in the cache or invokes the loader function to generate the entry if no entry is found. You can store an object up to 128 KB in the cache. If the value returned by the loader function is too large, the value isn't stored in the cache but dw.system.Cache.get(key, loader) still returns it. In this case, a log message is written to the custom warn log, and the operation is a "write failure" in the access statistics. No exception is thrown.

The cache can store only JavaScript primitive values and tree-like object structures. A cache can store null but not undefined. Objects retrieved from cache are immutable copies of the entries stored in the cache.

Custom cache content is stored in transient memory. Data is not persisted, and there is no guarantee that an entry can be retrieved from the cache. The storage allocated for entries is limited, and clearing or invalidation could occur at any time.

When using a custom cache, keep in mind that caches in different application servers of the same instance are separate. The dw.system.Cache.invalidate(key) method clears an entry only for the current application server.

For more information about dw.system.CacheMgr and dw.system.Cache, see B2C Commerce Script.