microsoft-authentication-library-for-python icon indicating copy to clipboard operation
microsoft-authentication-library-for-python copied to clipboard

TokenCache.add calls .modify inefficiently

Open chlowell opened this issue 5 years ago • 1 comments

TokenCache.__add pulls cacheable items from the given event and calls modify individually for each item. A single call to TokenCache.add may therefore entail up to 5 modify calls. This can degrade performance for subclasses of TokenCache, in particular PersistedTokenCache, whose modify method acquires a file lock. Handling a single response from AAD can therefore require acquiring this lock up to 5 times.

(I opened this here rather than in the msal-extensions repo because I expect TokenCache subclasses typically want to inherit TokenCache.add)

chlowell avatar Sep 17 '20 23:09 chlowell

Such observation is valid. We could use some improvement here. Lucky that, in the expected calling pattern, acquire_token_silent(...) would typically hit a token from cache, so the expensive TokenCache.add() would not be called more often than once an hour.

Marking this as an enhancement here. The actual change may still go into the extensions repo, though.

rayluo avatar Sep 18 '20 01:09 rayluo