mirror of
https://github.com/osukey/osukey.git
synced 2025-07-01 16:29:58 +09:00
Add and consume user cache class
This commit is contained in:
118
osu.Game/Database/UserLookupCache.cs
Normal file
118
osu.Game/Database/UserLookupCache.cs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using osu.Framework.Allocation;
|
||||||
|
using osu.Game.Online.API;
|
||||||
|
using osu.Game.Online.API.Requests;
|
||||||
|
using osu.Game.Users;
|
||||||
|
|
||||||
|
namespace osu.Game.Database
|
||||||
|
{
|
||||||
|
public class UserLookupCache : MemoryCachingComponent<int, User>
|
||||||
|
{
|
||||||
|
private readonly ConcurrentBag<int> nextTaskIDs = new ConcurrentBag<int>();
|
||||||
|
|
||||||
|
[Resolved]
|
||||||
|
private IAPIProvider api { get; set; }
|
||||||
|
|
||||||
|
private readonly object taskAssignmentLock = new object();
|
||||||
|
|
||||||
|
private Task<List<User>> pendingRequest;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether <see cref="pendingRequest"/> has already grabbed its IDs.
|
||||||
|
/// </summary>
|
||||||
|
private bool pendingRequestConsumedIDs;
|
||||||
|
|
||||||
|
public Task<User> GetUser(int userId, CancellationToken token = default) => GetAsync(userId, token);
|
||||||
|
|
||||||
|
protected override async Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)
|
||||||
|
{
|
||||||
|
var users = await getQueryTaskForUser(lookup);
|
||||||
|
return users.FirstOrDefault(u => u.Id == lookup);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return the task responsible for fetching the provided user.
|
||||||
|
/// This may be part of a larger batch lookup to reduce web requests.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">The user to lookup.</param>
|
||||||
|
/// <returns>The task responsible for the lookup.</returns>
|
||||||
|
private Task<List<User>> getQueryTaskForUser(int userId)
|
||||||
|
{
|
||||||
|
lock (taskAssignmentLock)
|
||||||
|
{
|
||||||
|
nextTaskIDs.Add(userId);
|
||||||
|
|
||||||
|
// if there's a pending request which hasn't been started yet (and is not yet full), we can wait on it.
|
||||||
|
if (pendingRequest != null && !pendingRequestConsumedIDs && nextTaskIDs.Count < 50)
|
||||||
|
return pendingRequest;
|
||||||
|
|
||||||
|
return queueNextTask(nextLookup);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<User> nextLookup()
|
||||||
|
{
|
||||||
|
int[] lookupItems;
|
||||||
|
|
||||||
|
lock (taskAssignmentLock)
|
||||||
|
{
|
||||||
|
pendingRequestConsumedIDs = true;
|
||||||
|
lookupItems = nextTaskIDs.ToArray();
|
||||||
|
nextTaskIDs.Clear();
|
||||||
|
|
||||||
|
if (lookupItems.Length == 0)
|
||||||
|
{
|
||||||
|
queueNextTask(null);
|
||||||
|
return new List<User>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = new GetUsersRequest(lookupItems);
|
||||||
|
|
||||||
|
// rather than queueing, we maintain our own single-threaded request stream.
|
||||||
|
request.Perform(api);
|
||||||
|
|
||||||
|
return request.Result.Users;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Queues new work at the end of the current work tasks.
|
||||||
|
/// Ensures the provided work is eventually run.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="work">The work to run. Can be null to signify the end of available work.</param>
|
||||||
|
/// <returns>The task tracking this work.</returns>
|
||||||
|
private Task<List<User>> queueNextTask(Func<List<User>> work)
|
||||||
|
{
|
||||||
|
lock (taskAssignmentLock)
|
||||||
|
{
|
||||||
|
if (work == null)
|
||||||
|
{
|
||||||
|
pendingRequest = null;
|
||||||
|
pendingRequestConsumedIDs = false;
|
||||||
|
}
|
||||||
|
else if (pendingRequest == null)
|
||||||
|
{
|
||||||
|
// special case for the first request ever.
|
||||||
|
pendingRequest = Task.Run(work);
|
||||||
|
pendingRequestConsumedIDs = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// append the new request on to the last to be executed.
|
||||||
|
pendingRequest = pendingRequest.ContinueWith(_ => work());
|
||||||
|
pendingRequestConsumedIDs = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pendingRequest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -61,6 +61,8 @@ namespace osu.Game
|
|||||||
|
|
||||||
protected BeatmapDifficultyCache DifficultyCache;
|
protected BeatmapDifficultyCache DifficultyCache;
|
||||||
|
|
||||||
|
protected UserLookupCache UserCache;
|
||||||
|
|
||||||
protected SkinManager SkinManager;
|
protected SkinManager SkinManager;
|
||||||
|
|
||||||
protected RulesetStore RulesetStore;
|
protected RulesetStore RulesetStore;
|
||||||
@ -229,6 +231,9 @@ namespace osu.Game
|
|||||||
dependencies.Cache(DifficultyCache = new BeatmapDifficultyCache());
|
dependencies.Cache(DifficultyCache = new BeatmapDifficultyCache());
|
||||||
AddInternal(DifficultyCache);
|
AddInternal(DifficultyCache);
|
||||||
|
|
||||||
|
dependencies.Cache(UserCache = new UserLookupCache());
|
||||||
|
AddInternal(UserCache);
|
||||||
|
|
||||||
var scorePerformanceManager = new ScorePerformanceCache();
|
var scorePerformanceManager = new ScorePerformanceCache();
|
||||||
dependencies.Cache(scorePerformanceManager);
|
dependencies.Cache(scorePerformanceManager);
|
||||||
AddInternal(scorePerformanceManager);
|
AddInternal(scorePerformanceManager);
|
||||||
|
@ -8,8 +8,8 @@ using osu.Framework.Bindables;
|
|||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Screens;
|
using osu.Framework.Screens;
|
||||||
|
using osu.Game.Database;
|
||||||
using osu.Game.Online.API;
|
using osu.Game.Online.API;
|
||||||
using osu.Game.Online.API.Requests;
|
|
||||||
using osu.Game.Online.Spectator;
|
using osu.Game.Online.Spectator;
|
||||||
using osu.Game.Screens.Multi.Match.Components;
|
using osu.Game.Screens.Multi.Match.Components;
|
||||||
using osu.Game.Screens.Play;
|
using osu.Game.Screens.Play;
|
||||||
@ -45,6 +45,9 @@ namespace osu.Game.Overlays.Dashboard
|
|||||||
[Resolved]
|
[Resolved]
|
||||||
private IAPIProvider api { get; set; }
|
private IAPIProvider api { get; set; }
|
||||||
|
|
||||||
|
[Resolved]
|
||||||
|
private UserLookupCache users { get; set; }
|
||||||
|
|
||||||
protected override void LoadComplete()
|
protected override void LoadComplete()
|
||||||
{
|
{
|
||||||
base.LoadComplete();
|
base.LoadComplete();
|
||||||
@ -55,18 +58,19 @@ namespace osu.Game.Overlays.Dashboard
|
|||||||
switch (e.Action)
|
switch (e.Action)
|
||||||
{
|
{
|
||||||
case NotifyCollectionChangedAction.Add:
|
case NotifyCollectionChangedAction.Add:
|
||||||
var request = new GetUsersRequest(e.NewItems.OfType<int>().ToArray());
|
|
||||||
|
|
||||||
request.Success += users => Schedule(() =>
|
foreach (var id in e.NewItems.OfType<int>().ToArray())
|
||||||
{
|
{
|
||||||
foreach (var user in users.Users)
|
users.GetUser(id).ContinueWith(u =>
|
||||||
{
|
{
|
||||||
if (playingUsers.Contains(user.Id))
|
Schedule(() =>
|
||||||
userFlow.Add(createUserPanel(user));
|
{
|
||||||
}
|
if (playingUsers.Contains(u.Result.Id))
|
||||||
});
|
userFlow.Add(createUserPanel(u.Result));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
api.Queue(request);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NotifyCollectionChangedAction.Remove:
|
case NotifyCollectionChangedAction.Remove:
|
||||||
|
Reference in New Issue
Block a user