# 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](/docs/commerce/b2c-commerce/guide/caching-strategies-sk.md).

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](https://salesforcecommercecloud.github.io/b2c-dev-doc/docs/current/scriptapi/html/index.html?target=class_dw_system_Response.html#dw_system_Response_setExpires_Number_DetailAnchor) to set the cache time.
- Use the `dw.system.Response#setVaryBy(identifier)` Script API method [as described here](https://salesforcecommercecloud.github.io/b2c-dev-doc/docs/current/scriptapi/html/index.html?target=class_dw_system_Response.html#dw_system_Response_setVaryBy_String_DetailAnchor) 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:

```javascript
var RESTResponseMgr = require("dw/system/RESTResponseMgr");

exports.getCustomProduct = function () {
  var customProduct = // ... some lookup of custom product data ...
    // set cache time to 60 seconds
    response.setExpires(Date.now() + 60000);

  RESTResponseMgr.createSuccess(customProduct).render();
};

exports.getCustomProduct.public = true;
```

**Example 2:** An implementation of a custom product API that caches responses based on promotion:

```javascript
var RESTResponseMgr = require("dw/system/RESTResponseMgr");

exports.getCustomProduct = function () {
  var customProduct = // ... some lookup of custom product data ...
    // set caching based on promotion
    response.setVaryBy("price_promotion");

  RESTResponseMgr.createSuccess(customProduct).render();
};

exports.getCustomProduct.public = true;
```
