Implementing custom caches with CacheMgr
Key Takeaway
Define custom caches in a JSON file in a custom cartridge, get one with CacheMgr.getCache(cacheID), fill it with get(key, loader), and never count on an entry still being there.
A custom cache is a named, in-memory store that B2C Commerce script code uses to keep data across requests, so the code doesn't repeat slow or expensive work every time. You define each cache in a JSON file in a custom cartridge. You read and write entries with the dw.system.CacheMgr and dw.system.Cache script API classes. In Business Manager, you turn caching on or off, watch cache statistics and clear caches.
When to use a custom cache
Use a custom cache for data that is expensive to calculate, slow to retrieve or accessed often. Typical cases include:
- Values that cost a lot to compute but change too often to persist. An example is checking every variation product for a promotion to show an On Sale banner on the base product.
- Responses from an external system, such as in-store availability and prices.
- Configuration settings loaded from a source like a JSON file in a versioning system.