1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Add a very simple user cache to ScoreImporter

This commit is contained in:
Dean Herbert 2023-06-09 19:03:02 +09:00
parent fbbeb3893b
commit c5e77e13de

View File

@ -146,16 +146,48 @@ namespace osu.Game.Scoring
#pragma warning restore CS0618
}
// Very naive local caching to improve performance of large score imports (where the username is usually the same for most or all scores).
private readonly Dictionary<string, APIUser> usernameLookupCache = new Dictionary<string, APIUser>();
protected override void PostImport(ScoreInfo model, Realm realm, ImportParameters parameters)
{
base.PostImport(model, realm, parameters);
var userRequest = new GetUserRequest(model.RealmUser.Username);
populateUserDetails(model);
}
/// <summary>
/// Legacy replays only store a username.
/// This will populate a user ID during import.
/// </summary>
private void populateUserDetails(ScoreInfo model)
{
string username = model.RealmUser.Username;
if (usernameLookupCache.TryGetValue(username, out var existing))
{
model.User = existing;
return;
}
var userRequest = new GetUserRequest(username);
api.Perform(userRequest);
if (userRequest.Response is APIUser user)
{
usernameLookupCache.TryAdd(username, new APIUser
{
// Because this is a permanent cache, let's only store the pieces we're interested in,
// rather than the full API response. If we start to store more than these three fields
// in realm, this should be undone.
Id = user.Id,
Username = user.Username,
CountryCode = user.CountryCode,
});
model.User = user;
}
}
}
}