Caching
Custom APIs support the caching of responses. Using this feature enhances the performance by reducing the need to repeatedly retrieve or compute data, resulting in faster response times and improved overall system efficiency. For more information on caching strategies, see Caching Strategies for Salesforce B2C Commerce.
To use caching for your Custom API endpoints, enable Page Caching for the sites using the Custom API. After that, a response can be cached in the implementation script in these ways:
- Use the
dw.system.Response#setExpires(milliseconds)Script API method as described here to set the cache time. - Use the
dw.system.Response#setVaryBy(identifier)Script API method as described here to mark the response as personalized. Use this with caution as it might result in performance degradation in places where the response is not actually personalized.
Example 1: An implementation of a custom product API that caches responses for 60 seconds:
Example 2: An implementation of a custom product API that caches responses based on promotion:
Starting with the B2C Commerce 26.9 release, the CDN layer can also cache Custom API responses, in front of the server-side web-tier cache. For details about how the CDN caching layer works, see SCAPI CDN Caching.
CDN caching applies only to requests that carry the personalized=none query parameter. For a Custom API to use CDN caching, the API’s schema definition must declare the personalized query parameter, so that clients can send personalized=none.
Declare the parameter on the relevant endpoint in the Custom API schema definition:
After the parameter is declared and a request includes personalized=none, the response becomes eligible for CDN caching on the same basis as system Shopper APIs. The caching mechanics (enablement, TTL via setExpires, and invalidation) remain as described above and in SCAPI CDN Caching.