-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jmprieur/perf with scalable Public Client Application cache #614
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
tests/Perf/Microsoft.Identity.Web.Perf.Client/ScalableTokenCacheHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Identity.Client; | ||
|
||
namespace Microsoft.Identity.Web.Perf.Client | ||
{ | ||
static class ScalableTokenCacheHelper | ||
{ | ||
/// <summary> | ||
/// Path to the token cache | ||
/// </summary> | ||
public static readonly string s_cacheFileFolder = | ||
Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), | ||
"TokenCaches"); | ||
public static readonly string s_cacheKeysFolder = s_cacheFileFolder + "Keys"; | ||
|
||
static ScalableTokenCacheHelper() | ||
{ | ||
if (!Directory.Exists(s_cacheFileFolder)) | ||
{ | ||
Directory.CreateDirectory(s_cacheFileFolder); | ||
} | ||
if (!Directory.Exists(s_cacheKeysFolder)) | ||
{ | ||
Directory.CreateDirectory(s_cacheKeysFolder); | ||
} | ||
} | ||
|
||
public static Dictionary<int, string> GetAccountIdsByUserNumber() | ||
{ | ||
int start = "MIWTestUser".Length; | ||
Dictionary<int, string> accountIdByUserNumber = new Dictionary<int, string>(); | ||
|
||
foreach(string filePath in Directory.EnumerateFiles(s_cacheKeysFolder)) | ||
{ | ||
string fileName = Path.GetFileName(filePath); | ||
string[] segments = fileName.Split('-'); | ||
string userUpn = segments[0]; | ||
string number = userUpn.Substring(start, userUpn.IndexOf('@')-start); | ||
|
||
accountIdByUserNumber.Add(int.Parse(number), string.Join("-", segments.Skip(1))); | ||
} | ||
return accountIdByUserNumber; | ||
} | ||
|
||
|
||
public static void BeforeAccessNotification(TokenCacheNotificationArgs args) | ||
{ | ||
string cacheFilePath = GetCacheFilePath(args); | ||
args.TokenCache.DeserializeMsalV3(File.Exists(cacheFilePath) | ||
? File.ReadAllBytes(cacheFilePath) | ||
: null); | ||
} | ||
|
||
private static string GetCacheFilePath(TokenCacheNotificationArgs args) | ||
{ | ||
string suggestedKey = args.SuggestedCacheKey ?? args.Account.HomeAccountId.Identifier; | ||
if (suggestedKey == null) | ||
{ | ||
return null; | ||
} | ||
return Path.Combine(s_cacheFileFolder, suggestedKey); | ||
} | ||
|
||
public static void AfterAccessNotification(TokenCacheNotificationArgs args) | ||
{ | ||
// if the access operation resulted in a cache update | ||
if (args.HasStateChanged) | ||
{ | ||
string cacheFilePath = GetCacheFilePath(args); | ||
|
||
// reflect changesgs in the persistent store | ||
File.WriteAllBytes(cacheFilePath, | ||
args.TokenCache.SerializeMsalV3()); | ||
|
||
WriteKey(args); | ||
} | ||
} | ||
|
||
private static void WriteKey(TokenCacheNotificationArgs args) | ||
{ | ||
if (args.Account != null) | ||
{ | ||
string keyPath = Path.Combine(s_cacheKeysFolder, | ||
args.Account.Username + "-" + args.Account.HomeAccountId.Identifier); | ||
if (!File.Exists(keyPath)) | ||
{ | ||
File.WriteAllText(keyPath, " "); | ||
} | ||
} | ||
} | ||
|
||
internal static void EnableSerialization(ITokenCache tokenCache) | ||
{ | ||
tokenCache.SetBeforeAccess(BeforeAccessNotification); | ||
tokenCache.SetAfterAccess(AfterAccessNotification); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,29 +14,21 @@ static class TokenCacheHelper | |
/// </summary> | ||
public static readonly string s_cacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalcache.bin3"; | ||
|
||
private static readonly object s_fileLock = new object(); | ||
|
||
public static void BeforeAccessNotification(TokenCacheNotificationArgs args) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The lock is not necessary and takes time There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i already removed that |
||
lock (s_fileLock) | ||
{ | ||
args.TokenCache.DeserializeMsalV3(File.Exists(s_cacheFilePath) | ||
? File.ReadAllBytes(s_cacheFilePath) | ||
: null); | ||
} | ||
} | ||
|
||
public static void AfterAccessNotification(TokenCacheNotificationArgs args) | ||
{ | ||
// if the access operation resulted in a cache update | ||
if (args.HasStateChanged) | ||
{ | ||
lock (s_fileLock) | ||
{ | ||
// reflect changesgs in the persistent store | ||
File.WriteAllBytes(s_cacheFilePath, | ||
args.TokenCache.SerializeMsalV3()); | ||
} | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recreating a PublicClientApplication each time to avoid the MSAL.NET performance bug which makes the cache grow with the number of users even if we store it in a different file (singleton)