1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00

Add ability to lookup multiple users at once to UserLookupCache

This commit is contained in:
Dean Herbert 2021-07-05 19:56:16 +09:00
parent 10e7c846e5
commit 9c311a6d8a
2 changed files with 25 additions and 20 deletions

View File

@ -27,6 +27,30 @@ namespace osu.Game.Database
[ItemCanBeNull]
public Task<User> GetUserAsync(int userId, CancellationToken token = default) => GetAsync(userId, token);
/// <summary>
/// Perform an API lookup on the specified users, populating a <see cref="User"/> model.
/// </summary>
/// <param name="userIds">The users to lookup.</param>
/// <param name="token">An optional cancellation token.</param>
/// <returns>.</returns>
public Task<User[]> GetUsersAsync(int[] userIds, CancellationToken token = default)
{
var userLookupTasks = new List<Task<User>>();
foreach (var u in userIds)
{
userLookupTasks.Add(GetUserAsync(u, token).ContinueWith(task =>
{
if (!task.IsCompletedSuccessfully)
return null;
return task.Result;
}, token));
}
return Task.WhenAll(userLookupTasks);
}
protected override async Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)
=> await queryUser(lookup).ConfigureAwait(false);

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
@ -61,7 +60,7 @@ namespace osu.Game.Screens.Spectate
{
base.LoadComplete();
getAllUsers().ContinueWith(users => Schedule(() =>
userLookupCache.GetUsersAsync(userIds.ToArray()).ContinueWith(users => Schedule(() =>
{
foreach (var u in users.Result)
userMap[u.Id] = u;
@ -77,24 +76,6 @@ namespace osu.Game.Screens.Spectate
}));
}
private Task<User[]> getAllUsers()
{
var userLookupTasks = new List<Task<User>>();
foreach (var u in userIds)
{
userLookupTasks.Add(userLookupCache.GetUserAsync(u).ContinueWith(task =>
{
if (!task.IsCompletedSuccessfully)
return null;
return task.Result;
}));
}
return Task.WhenAll(userLookupTasks);
}
private void beatmapUpdated(ValueChangedEvent<WeakReference<BeatmapSetInfo>> e)
{
if (!e.NewValue.TryGetTarget(out var beatmapSet))