Skip to content

Conversation

@paulcaru
Copy link

Improving GetToken Method by removing lock and letting Microsoft Memory Cache handle this as it wasn't handled well for concurrency

paulcaru and others added 5 commits April 18, 2024 19:45
1) All Waiting for Lock if Token is Null
2) First goes in Checks Again Memory Cache
3) Nothing is found and calls GetTokenFromServer and fetches token
4) Lock is released and 2nd one goes in. checks Memory Cache and its found
5) Return Token
1) All Waiting for Lock if Token is Null
2) First goes in Checks Again Memory Cache
3) Nothing is found and calls GetTokenFromServer and fetches token
4) Lock is released and 2nd one goes in. checks Memory Cache and its found
5) Return Token
}
return RefreshToken(authenticationSettings, tokenClient);

lock (_getTokenlockObject)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this should be used:

string token = _memoryCache.GetOrCreate(authenticationSettings, entry =>
{
    entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
    
    return GetTokenFromServer(authenticationSettings, tokenClient);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants