Token Cache Aspect implementation - AzureAD/microsoft-authentication-extensions-for-java GitHub Wiki
- MSAL SDK can be configured with an instance of ITokenCacheAccessAspect interface.
It is aspect which wraps token cache access inside MSAL SDK.
public interface ITokenCacheAccessAspect {
void beforeCacheAccess(ITokenCacheAccessContext context);
void afterCacheAccess(ITokenCacheAccessContext context);
}
- ITokenCacheAccessAspect implementation
In order to have persistent cache, application developer supposed to implement this interface.
BeforeCacheAccess method supposed to read persistent state of the cache and deserialize it to MSAL in-memory cache. AfterCacheAccess method supposed to serialize in-memory MSAL cache and write it to persistent storage.
- Implementation is supposed to handle concurrent access to the persistent storage.
In general case, cross-process lock should be acquired in beforeCacheAccess method and release it in afterCacheAccess method.
BUT if wrapped MSAL cache access logic is "read access", lock should be released in BeforeCacheAccess method in order to avoid lock starvation.
Another way to optimize cache read operations is to trek version of persisted cache state to avoid unnecessary lock acquisitions.
Majority of cache operation is reads, so optimizing it is crucial.