Forum Discussion

isxtn's avatar
isxtn
Advisor
11 months ago

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...
  • isxtn's avatar
    11 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>