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: