isxtn
10 months agoAdvisor
Can I cache the JSON returned by an endpoint? Would like to not make the AJAX or API call repeatedly
I have an endpoint that calls an external API which returns JSON. It's for "related content" for a post. (Yes, I know Khoros has an OOTB and a componentized version of that already for Hermes, but I...
- 10 months ago
Just adding I think luk answers this here (maybe)
https://community.khoros.com/t5/Developer-Discussion/Advice-on-performance-for-external-API-query/m-p/208835/highlight/true#M9494 - 10 months ago
How I resolved this with a little input from here and elsewhere. I have always found the appcache confusing so I needed some help. Thanks.
<#if page.context?? && page.context.message??> <#assign myKey = "myKey_"+page.context.message.uniqueId /> <#else> <#assign myKey = "xyz" /> </#if> <#if appcache.get("myKey", "--EMPTY--") == "--EMPTY--"> <#assign apiURL = "https://stage.apiurl.com/api/id/A1B2C3d4" /> <#assign apiResponse = http.client.request("https","stage.apiurl.com","/api/id/A1B2C3d4") .header("authorization", "APIKey whateveryourA4IK3Yis" ) .header("content-type", "application/json;charset=UTF-8" ) </> <#assign apiResponse = apiResponse.post() /> <#assign content = apiResponse.content /> ${appcache.put("myKey", content)} ${content} <#else> ${appcache.get("myKey")} </#if>