Skip to content

Added caching for GetAccessToken() method #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion ebay-oauth-csharp-client/eBay/ApiClient/Auth/OAuth2/OAuth2Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class OAuth2Api
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static AppTokenCache appTokenCache = new AppTokenCache();
private static AppTokenCache accessTokenCache = new AppTokenCache();

private class AppTokenCache
{
Expand Down Expand Up @@ -206,7 +207,23 @@ public OAuthResponse GetAccessToken(OAuthEnvironment environment, String refresh
};
String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

OAuthResponse oAuthResponse = FetchToken(environment, requestPayload, TokenType.USER);
OAuthResponse oAuthResponse;

//Check for token in cache
oAuthResponse = accessTokenCache.GetValue(environment);
if (oAuthResponse != null && oAuthResponse.AccessToken != null && oAuthResponse.AccessToken.Token != null)
{
log.Info("Returning access token from cache for " + environment.ConfigIdentifier());
return oAuthResponse;
}

oAuthResponse = FetchToken(environment, requestPayload, TokenType.USER);

if (oAuthResponse != null && oAuthResponse.AccessToken != null)
{
accessTokenCache.UpdateValue(environment, oAuthResponse, oAuthResponse.AccessToken.ExpiresOn);
}

return oAuthResponse;
}

Expand Down