Leveraging Server-Side Caching to Improve SFCC REST API Speed

The OCAPI has been around for a long time (2016) and allows you to cache responses to increase performance. By default, GET responses that support caching are cached for 60 seconds, but can this be improved? 

What can be cached in the OCAPI?

Before we start, we must understand that not all API endpoints support caching. But which ones do?

 
This is quite an extensive list and contains all the objects we would expect to support caching!

Page Cache

An important thing to remember before starting to tinker with the Shop API (part of the OCAPI) caching is to enable the “Page Cache” for the site you will be working with.

If the Page Cache is disabled, you will see this header value on every response:

				
					cache-control: no-cache, no-store, must-revalidate
				
			

This is easy to fix. But without enabling it, you cannot test your settings on a sandbox where this is usually disabled.

Overriding the OCAPI Cache Time

It is possible to override the default 60 seconds of caching of an resource by adding it to the OCAPI Settings in the Business Manager.

“Administration” > “Site Development” > “Open Commerce API Settings”

				
					{
	"_v": "22.6",
	"clients": [
		{
			"client_id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
			"allowed_origins": [],
			"resources": [
				{
					"resource_id": "/categories/*",
					"methods": [
						"get"
					],
					"read_attributes": "(**)",
					"cache_time": 900
				},
				{
					"resource_id": "/customers/auth",
					"methods": [
						"post"
					],
					"read_attributes": "(**)",
					"write_attributes": "(**)"
				},
				{
					"resource_id": "/product_search",
					"methods": [
						"get"
					],
					"read_attributes": "(**)",
					"write_attributes": "(**)",
                    "cache_time": 86400
				}
			]
		}
	]
}
				
			

Adding “cache_time” to the resource configuration lets you easily control the time responses are cached.

You can set a maximum value of 86.400 seconds (1 day).

"Expand" parameter

OCAPI: Expand Parameter Caching
Screenshot of the Infocenter about the "expand" parameter

Personalized Caching

Personalized caching is enabled by default based on the customer context (JWT). It is possible to disable this for a resource to improve performance.

				
					{
	"_v": "22.6",
	"clients": [
		{
			"client_id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
			"allowed_origins": [],
			"resources": [
				{
					"resource_id": "/product_search",
					"methods": [
						"get"
					],
					"read_attributes": "(**)",
					"write_attributes": "(**)",
					"cache_time": 86400,
					"personalized_caching_enabled": false
				}
			]
		}
	]
}
				
			

By setting the “personalized_caching_enabled” option to false, personalization will be disabled for that resource.

SCAPI (Salesforce Commerce API)

Currently, you can’t control the server-side cache times of SCAPI. All known approaches from the OCAPI Shop API (for example, setting cache times in the OCAPI settings) don’t apply to the Salesforce Commerce API.

Custom Caches to the rescue (for hooks)!

Custom caches are user-defined in Salesforce B2C Commerce Cloud, allowing developers to store and retrieve data efficiently. They can be used to cache frequently accessed data, reducing the load on the server and speeding up response times for SCAPI REST APIs where hooks have been implemented.

Here are some ways custom caches can be used to speed up responses in the SCAPI REST APIs that have customisations:

  1. Reducing database queries: If an API call requires fetching data from the database, custom caches can help store the data in memory. This way, when subsequent API calls are made, the data can be quickly retrieved from the cache instead of querying the database again.

  2. Complex Calculations: Sometimes, processing API requests may involve complex calculations or transformations. Custom caches can store the results of these calculations, allowing subsequent requests to retrieve the cached data instead of re-computing the results.

  3. Third-party API responses: If your SCAPI REST APIs depend on third-party APIs, custom caches can help store the responses from these external APIs, reducing latency and improving performance.

OCAPI Caching Best Practices

There is a lot of information and best practices available on the Infocenter.

Caching

Table of Contents

Facebook
Twitter
LinkedIn

How to use OCAPI/SCAPI hooks

As the Composable Storefront (PWA Kit) and SCAPI/OCAPI are being used increasingly, the need to customise these endpoints becomes a basic necessity. How can we

Read More »