Forum Discussion

keithkelly's avatar
11 months ago

Possible to Cache the Output of an Endpoint?

I have

  1. a custom endpoint that returns custom KPIs per board. 
  2. Custom board lists on the Community Home page.

For Board Oversight teams, leadership , etc. I want to post the board-level KPIs (#1) directly on the home page's board lists (#2).

Bad (current) way of doing it:  Calculate KPI data for each board...each and every time the board list is generated.

Can I EITHER:

  1. Cache the KPI endpoint response, OR
  2. Cache each individual metric as its looked up (cache key would be the board ID)

 

  • TrippB's avatar
    TrippB
    Khoros Alumni (Retired)

    Hi keithkelly , you could definitely do this. You have a couple of options, appcache, usercache, and potentially component caching.

    appcache is great for this but keep security in mind. The cached data should only be accessed by your endpoint and make sure that your endpoint has user and role checks to prevent unauthorised access to this information. Give your data a key that won't be used by another developer for another purpose so that you don't inadvertently expose the KPI data.

    You can store structured data like a JSON object (in freemarker, it's called a "hashmap" and you can use the json methods of the utils Freemarker context object to convert the hashmap to a string to serialize the data out to the response stream for the caller.

    Take a look a the appcache context object documentation, the utils context object documentation, and the component caching documentation. You should be able to put something together based on this information. Good luck!