I have a table with data that is retrieved from API via community connector. Also I have a fixed list control filled with data from some field of the same community connector that is used as a filter for the table. Every time I select an item from the control a new call of getDate is happen. Is there any trick to prevent to make these extra calls of getData?
0
votes
1 Answers
0
votes
You can implement Cache Class to prevent extra calls to your API.
function getCampaign(campaign_id) {
var endpoint = DOMAIN + GET_CAMPAIGN_ENDPOINT;
// Make a POST request with form data.
var formData = {
'campaign_id': campaign_id,
'with_statistics': true
};
var cache = CacheService.getScriptCache();
var cacheKey = generateCacheKey(endpoint, formData); // generate key for the request
var json = cache.get(cacheKey);
if (json != null) {
console.log('from cache');
return JSON.parse(json);
}
var options = {
'method' : 'post',
'payload' : formData
};
var response = UrlFetchApp.fetch(endpoint, options);
var json = response.getContentText();
cache.put(cacheKey, json, 1500); // cache for 25 minutes
console.log('from api');
return JSON.parse(json);
}
function generateCacheKey(endpoint, formData){
var s = endpoint + JSON.stringify(formData);
return Utilities.base64Encode(s);
}