1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-16 20:43:12 +08:00
Files
osu-lazer/osu.Game/Database/MemoryCachingComponent.cs
T
Dan Balasescu 1230da33a5 Implement sorting and display styles for currently online users (#33649)
- Adds sorting and display styles.
- Saves sort/display modes to the config.
- Improves performance, especially on the 2nd+ time opening the overlay.


https://github.com/user-attachments/assets/e32b50d0-58a1-4eef-b18c-988fb497e545

---

Coming off some recent feedback in
https://github.com/ppy/osu/discussions/33426#discussioncomment-13431275,
I decided to take a bit of a detour and get a little bit more
functionality in.

Sorting by rank, although it should technically work, doesn't work right
now. This is because the osu!web API doesn't return user rank on
`/user/` lookups - it's only returned for the friends request. I'm
leaving this open as a discussion topic.
- We can make osu!web return the rank and osu! will require no further
changes to work correctly, or
- We can try to implement additional paths through
`osu-server-spectator` which would blow this PR out of proportion and is
best left for a task of its own.

For simplicity, I've re-implemented this display mostly as its own
component for now, lifting code from `FriendDisplay` which was recently
overhauled. These implementations should eventually be combined somehow
but that's dependent on:
1. Figuring out the styling - friends can display offline users for
which it makes no sense to display the "spectate" button.
2. Figuring out how to handle the different users/presence pathways.
It's mostly a code complexity issue.

---------

Co-authored-by: Dean Herbert <pe@ppy.sh>
Co-authored-by: Bartłomiej Dach <dach.bartlomiej@gmail.com>
2026-02-05 01:22:36 +09:00

126 lines
4.6 KiB
C#

// 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.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Graphics;
using osu.Framework.Statistics;
namespace osu.Game.Database
{
/// <summary>
/// A component which performs lookups (or calculations) and caches the results.
/// Currently not persisted between game sessions.
/// </summary>
public abstract partial class MemoryCachingComponent<TLookup, TValue> : Component
where TLookup : notnull
{
private readonly ConcurrentDictionary<TLookup, TValue?> cache = new ConcurrentDictionary<TLookup, TValue?>();
private readonly GlobalStatistic<MemoryCachingStatistics> statistics;
protected virtual bool CacheNullValues => true;
protected MemoryCachingComponent()
{
statistics = GlobalStatistics.Get<MemoryCachingStatistics>(nameof(MemoryCachingComponent<TLookup, TValue>), GetType().ReadableName());
statistics.Value = new MemoryCachingStatistics();
}
/// <summary>
/// Retrieve the cached value for the given lookup.
/// </summary>
/// <param name="lookup">The lookup to retrieve.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
/// <param name="computationDelay">In the case a cached lookup was not possible, a value in milliseconds of to wait until performing potentially intensive lookup.</param>
protected async Task<TValue?> GetAsync(TLookup lookup, CancellationToken cancellationToken = default, int computationDelay = 0)
{
if (CheckExists(lookup, out TValue? existing))
{
statistics.Value.HitCount++;
return existing;
}
if (computationDelay > 0)
await Task.Delay(computationDelay, cancellationToken).ConfigureAwait(false);
var computed = await ComputeValueAsync(lookup, cancellationToken).ConfigureAwait(false);
statistics.Value.MissCount++;
if (computed != null || CacheNullValues)
{
cache[lookup] = computed;
statistics.Value.Usage = cache.Count;
}
return computed;
}
/// <summary>
/// Invalidate all entries matching a provided predicate.
/// </summary>
/// <param name="matchKeyPredicate">The predicate to decide which keys should be invalidated.</param>
protected void Invalidate(Func<TLookup, bool> matchKeyPredicate)
{
foreach (var kvp in cache)
{
if (matchKeyPredicate(kvp.Key))
cache.TryRemove(kvp.Key, out _);
}
statistics.Value.Usage = cache.Count;
}
/// <summary>
/// Completely purge the cache.
/// </summary>
public virtual void Clear()
{
cache.Clear();
statistics.Value.Usage = 0;
}
protected bool CheckExists(TLookup lookup, [MaybeNullWhen(false)] out TValue value) =>
cache.TryGetValue(lookup, out value);
/// <summary>
/// Called on cache miss to compute the value for the specified lookup.
/// </summary>
/// <param name="lookup">The lookup to retrieve.</param>
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
/// <returns>The computed value.</returns>
protected abstract Task<TValue?> ComputeValueAsync(TLookup lookup, CancellationToken token = default);
private class MemoryCachingStatistics
{
/// <summary>
/// Total number of cache hits.
/// </summary>
public int HitCount;
/// <summary>
/// Total number of cache misses.
/// </summary>
public int MissCount;
/// <summary>
/// Total number of cached entities.
/// </summary>
public int Usage;
public override string ToString()
{
int totalAccesses = HitCount + MissCount;
double hitRate = totalAccesses == 0 ? 0 : (double)HitCount / totalAccesses;
return $"i:{Usage} h:{HitCount} m:{MissCount} {hitRate:0%}";
}
}
}
}