How to apply cache to API response at device, user or on all requests

How to apply cache to API response at device, user or on all requests

1 Like

@Vikas_Dhillon could you help on this?

There are two levels of caching that can be implemented in vConnect:

Response Cached for All Users:

When setting up a service in vConnect, there’s an option labeled “Is Caching Required.” By selecting ‘Yes’ for this option, you can define a cache time, which determines how long the response should be cached. For instance, if you set a cache time of 30 minutes, vConnect won’t make API calls for 30 minutes. Instead, it will return the response from the cache to all users. After this period, when a request comes in, vConnect will automatically fetch the response from the linked API and refresh the cache. It’s important to note that currently, there’s no provision to forcibly clear the cache before the specified time.

This caching method is ideal for scenarios where the response remains constant for all users. Examples include APIs that provide lists of countries, FAQs, or general configurations.

Response Cached Based on Input Parameters like userId:

Let’s consider a situation where you need to provide a list of cities based on states. In this case, the response depends on the input, specifically the StateId. For such scenarios, the API Tracing feature comes into play.

To enable API Tracing, set the field ‘API Tracing’ to ‘Yes’ and configure parameters like the ‘Record Id’ (Json Path of the Input Parameter). If caching needs to be tailored to multiple input parameters, use Json paths separated by the ~ tild character. For example, if your unique key is a combination of StateId and CountryId, the primary key would be defined as: $.stateId$~countryId.

How to configure API Tracing:

API Tracing: Yes
onSuccessAction: CACHE_RESPONSE
onFailureAction: CALL_API
expiryInMinutesRedis: 60 (Cache expiration time in minutes)
successRule: {‘$[0].RESPONSE_MESSAGE’:‘SUCCESS’}
primaryKey: $.stateId
primaryKey: $.stateId~$.countryId (for multiple input parameters)
Ensure that the success rule is properly configured to utilize this feature.

These configurations will help achieve caching based on specific input parameters. The API Tracing feature provides an effective way to optimize responses in scenarios where they depend on certain input values.