From 30171e9f7ea8c8c783a18815b2fb9cc0e9335829 Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Tue, 1 Oct 2019 16:23:54 +1000 Subject: [PATCH] Added caching for GetAccessToken() method --- .../eBay/ApiClient/Auth/OAuth2/OAuth2Api.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ebay-oauth-csharp-client/eBay/ApiClient/Auth/OAuth2/OAuth2Api.cs b/ebay-oauth-csharp-client/eBay/ApiClient/Auth/OAuth2/OAuth2Api.cs index d33496f..65ec9d6 100644 --- a/ebay-oauth-csharp-client/eBay/ApiClient/Auth/OAuth2/OAuth2Api.cs +++ b/ebay-oauth-csharp-client/eBay/ApiClient/Auth/OAuth2/OAuth2Api.cs @@ -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 { @@ -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; }