Exploring Custom Caching in Salesforce Commerce Cloud (SFCC)

CloudCommerce Academy
4 min readSep 6, 2023

--

Caching plays a crucial role in improving the performance of your Salesforce Commerce Cloud (SFCC) website. It allows you to store and retrieve data that is expensive to calculate, frequently accessed, or slow to fetch from external systems. In this article, we’ll dive deep into custom caching in SFCC, discussing what it is, how to define custom caches, and providing multiple code examples for usage. Before we get started, let’s set up our example scenario.

Setup Example

Before we explore custom caching in SFCC, let’s consider a scenario where we want to optimize the performance of an e-commerce site. Our example involves caching product information. Imagine you have a base product, and its appearance depends on the attributes of its variation products. Checking if any variation product is part of a promotion is a resource-intensive operation that we don’t want to repeat every time the base product is displayed. Instead, we will calculate this information and store it in a custom cache for a few minutes. Additionally, we’ll cache responses from an external system, such as in-store availability and prices.

What Is a Custom Cache?

In SFCC, approximately 20 MB of memory is reserved for all custom caches on each application server. Multiple application servers can exist within a single B2C Commerce instance, but custom caches on different application servers are separate and not synchronized across the instance.

Use Cases for Custom Caches:

  1. Expensive Calculations: Cache data that is costly to compute, like our example of checking if a product is part of a promotion.
  2. External System Responses: Cache responses from external systems to reduce the number of calls and improve page load times.
  3. Configuration Settings: Store configuration data from various sources, such as JSON files, in a custom cache for quick access.

Enabling Custom Caching

To enable custom caching, navigate to Administration > Operations > Custom Caches > Enable Caching. Keep caching enabled on production systems; you can disable it for testing purposes, but doing so will clear all custom caches on all application servers in the instance.

Defining a Custom Cache

  1. Create a JSON File: Define a custom cache in a cartridge using a JSON file. You can have up to 100 custom caches across all cartridges in a code version.
  2. Specify Cache Path: Reference the path of the cache definition JSON file in the cartridge’s package.json file.
  3. Example package.json entry:
"caches": "./caches.json"
  1. Assign Unique IDs: Assign each cache a unique ID across all cartridges in a code version. Duplicate IDs in different cartridges result in errors.
  2. Example caches.json:
{
"caches": [{

"id": "UnlimitedCaching"
}, {
"id": "LimitedCaching",
"expireAfterSeconds": 10
}]
}

Handling Cache Definition Problems

Invalid cache definitions are reported in the custom error log. Be sure to monitor this log for any issues.

Cache Clearing

Remember that changing any file in the active code version or activating a new code version clears the content of all custom caches in that version. Additionally, custom caches are cleared at the end of data replications or code replications.

Using Custom Caches

To work with custom caches in SFCC, you’ll use the dw.system.CacheMgr and dw.system.Cache classes.

Retrieving a Cache

Use the CacheMgr.getCache(cacheID) method to retrieve caches that are already defined.

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

Storing and Retrieving Cache Entries

Cache entries are identified by keys. Use the Cache.get(key) method to retrieve the object associated with the key if it exists.

var productKey = 'product123';
var productInfo = cache.get(productKey);

Populating a Cache

A best practice is to populate a cache using, 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.

var productKey = 'product123';
var productInfo = cache.get(productKey, function loadProductInfo() {
// Perform expensive calculation or fetch data from an external system
return calculateProductInfo(productKey);
});

Cache Limitations

  • You can store objects up to 128 KB in the cache.
  • The cache can store only JavaScript primitive values and tree-like object structures.
  • Custom cache content is stored in transient memory and isn’t persisted.
  • Caches on different application servers are separate, and you can only clear an entry for the current application server using dw.system.Cache.invalidate(key).

Conclusion

Custom caching in SFCC is a powerful tool to optimize the performance of your e-commerce site. Whether you’re caching expensive calculations or external system responses, custom caches can significantly reduce page load times and resource consumption. Remember to define your caches carefully, handle cache clearing, and utilize best practices when working with custom caches in SFCC.

For more in-depth information about dw.system.CacheMgr and dw.system.Cache, consult the official documentation.

Now, armed with this knowledge, you can supercharge your SFCC application’s performance with custom caching. Happy coding!

--

--

CloudCommerce Academy

With an extensive background in the IT industry, we possess a wealth of experience spanning multiple years, primarily concentrated in the Cloud Domain.