Token Caching with AEM | Community
Skip to main content
Level 3
November 11, 2022
Solved

Token Caching with AEM

  • November 11, 2022
  • 2 replies
  • 1434 views

Hi All,

 

We have a Rest API Integration with AEM and Before Hitting the actual API we hit a API which generates a Token and Token needs to be passed to actual API to get the Response.

 

So first time once API is hit to get the token ,token is valid for 10 minutes so within 10 min same token should be used for all the API's and should be saved Server Side.

 

What is the best way to store the Token in AEM at server Side.

 

Do we need to use any Java Cache like (Ehcache) or what is the correct approach to handle such sceanrios.

 

Thanks in Advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

Hi @sb2512 ,

I would use JavaBean which adhere to an extremely simple coding convention to cache response. 

  • Call API to get Token response.
    Interface
    public interface Response {
         String getToken();
    }
    Implementation
    public class TokenResponse implements Response {
    public String getToken() {
            if (null != this.getToken) {
                return this.getToken();
            }
            return null;
        }
    }
    Invoking
    TokenResponse tokenResponse = callYourHttpAPI();
     if (null ! = tokenResponse {
            //Do Something (Call to 2nd API by passing tokenResponse as a param)
            ..........
    }

Hope that helps!

Regards,
Santosh

2 replies

arunpatidar
Community Advisor
Community Advisor
November 11, 2022

Hi

You can use in memory cache or jcr node to store but if you have multiple instance, in that case the storage will not guaranty that the token will be available.

but if you just want to improve this in term of performance then yes, you can use in memory cache.

Arun Patidar
Sb2512Author
Level 3
November 11, 2022

Hi Arun,

 

Thanks for the reply...

 

If there are multiple publish instance how the token will be synced....

 

Any thoughts @arunpatidar 

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
November 11, 2022

Hi @sb2512 ,

I would use JavaBean which adhere to an extremely simple coding convention to cache response. 

  • Call API to get Token response.
    Interface
    public interface Response {
         String getToken();
    }
    Implementation
    public class TokenResponse implements Response {
    public String getToken() {
            if (null != this.getToken) {
                return this.getToken();
            }
            return null;
        }
    }
    Invoking
    TokenResponse tokenResponse = callYourHttpAPI();
     if (null ! = tokenResponse {
            //Do Something (Call to 2nd API by passing tokenResponse as a param)
            ..........
    }

Hope that helps!

Regards,
Santosh

Santosh Sai