Skip to content

[Prototype] Add partition-aware cache serializer#4070

Closed
pmaytak wants to merge 1 commit into
mainfrom
pmaytak/partitioned-cache-serializer
Closed

[Prototype] Add partition-aware cache serializer#4070
pmaytak wants to merge 1 commit into
mainfrom
pmaytak/partitioned-cache-serializer

Conversation

@pmaytak

@pmaytak pmaytak commented Apr 12, 2023

Copy link
Copy Markdown
Contributor

Related to #3285, #3439.

Adds a token cache serializer that accepts a cache partition key which allows to serialize only that specific partition and not the whole internal cache dictionary.

Example usage:

        public void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            if (!string.IsNullOrEmpty(args.SuggestedCacheKey))
            {
                byte[] tokenCacheBytes = ReadCacheBytes(args.SuggestedCacheKey);
                ((IPartitionedTokenCacheSerializer)args.TokenCache).DeserializeMsal(tokenCacheBytes);
            }
        }

        public void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            if (args.HasStateChanged)
            {
                if (args.HasTokens)
                {
                    var key = string.IsNullOrEmpty(args.SuggestedCacheKey) ? args.Account.HomeAccountId.Identifier : args.SuggestedCacheKey;
                    WriteCacheBytes(key, ((IPartitionedTokenCacheSerializer)args.TokenCache).SerializeMsal(key));
                }
                else
                {
                    // No token in the cache. we can remove the cache entry
                    RemoveKey(args.SuggestedCacheKey);
                }
            }
        }

@pmaytak pmaytak closed this Apr 13, 2023
@bgavrilMS bgavrilMS deleted the pmaytak/partitioned-cache-serializer branch April 25, 2024 13:52
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.

1 participant