Action api.cache doesn't store as expected

Hello,

I have a user creation flow that requires a m2m token to call my own API. To reduce the token requests due to the quota limitation, I wanted to cache the m2m token in api.cache. After reading all documentations on action caching and other posts in the community. I set up a simple structure to test if the caching works as desired before testing with actual tokens:

exports.onExecutePostLogin = async (event, api) => {
  let cache = api.cache.get('testCache');
  if(cache?.value == undefined){
    api.cache.set('testCache', 'testing')
    cache = api.cache.get('testCache');
  }
  api.idToken.setCustomClaim('apiCache', cache)
}

*** I added it to the token so I can see the cache result output when testing

Upon testing, I see the first line ‘api.cache.get(‘testCache’)’ almost always return a value of null, which triggers a new cache being set. My understanding to api.cache is that the correct caching should save and be reuse the value for 15 mins before regenerate/set a new cache. Can I get some assistance on what I’m doing wrong here for action cache? Thanks!

DLTR: I’m assuming I have similar issue as this topic My follow up question to that would be is there a better way to reach my external API through login/post login auth0 action without a M2M token?

Hi @lily.tai,

I have just reviewed and tested your Post Login action script and was able to successfully set and get the cache item without issues. In future calls, I could get the cached item correctly without setting a new cache item.

Are you calling the Management API with this M2M Token?

Hello @rueben.tiow ,

Thank you so much for checking my cacheing implementation! I’m calling my external API with this M2M token and was wondering if there’s ways to work around to reach my own API endpoints without using a M2M token? Or perhaps ways to generate a longer-living token/cache? Since now the limit for cache is 15 minutes and with load balancer having to store cache in different nodes, cacheing doesn’t seem to be the best solution. Any guidance or suggestion would be much appreciated! Thank you.

Hi @lily.tai,

Thanks for the reply.

Could you provide me a code sample of how you are calling your external API with your M2M token?

Thanks,
Rueben