2020-11-06 12:26:18 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-06-24 17:39:44 +08:00
|
|
|
using System;
|
2020-11-06 12:26:18 +08:00
|
|
|
using System.Collections.Concurrent;
|
2023-06-09 17:24:38 +08:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-11-06 12:50:51 +08:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2022-07-06 17:38:39 +08:00
|
|
|
using osu.Framework.Extensions.TypeExtensions;
|
2020-11-06 12:26:18 +08:00
|
|
|
using osu.Framework.Graphics;
|
2022-07-06 17:38:39 +08:00
|
|
|
using osu.Framework.Statistics;
|
2020-11-06 12:26:18 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A component which performs lookups (or calculations) and caches the results.
|
|
|
|
/// Currently not persisted between game sessions.
|
|
|
|
/// </summary>
|
2022-11-24 13:32:20 +08:00
|
|
|
public abstract partial class MemoryCachingComponent<TLookup, TValue> : Component
|
2023-06-09 17:24:38 +08:00
|
|
|
where TLookup : notnull
|
2020-11-06 12:26:18 +08:00
|
|
|
{
|
2023-06-09 17:24:38 +08:00
|
|
|
private readonly ConcurrentDictionary<TLookup, TValue?> cache = new ConcurrentDictionary<TLookup, TValue?>();
|
2020-11-06 12:50:51 +08:00
|
|
|
|
2022-07-06 17:38:39 +08:00
|
|
|
private readonly GlobalStatistic<MemoryCachingStatistics> statistics;
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
protected virtual bool CacheNullValues => true;
|
|
|
|
|
2022-07-06 17:38:39 +08:00
|
|
|
protected MemoryCachingComponent()
|
|
|
|
{
|
|
|
|
statistics = GlobalStatistics.Get<MemoryCachingStatistics>(nameof(MemoryCachingComponent<TLookup, TValue>), GetType().ReadableName());
|
|
|
|
statistics.Value = new MemoryCachingStatistics();
|
|
|
|
}
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
/// <summary>
|
2020-11-06 13:31:21 +08:00
|
|
|
/// Retrieve the cached value for the given lookup.
|
2020-11-06 12:50:51 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="lookup">The lookup to retrieve.</param>
|
|
|
|
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
|
2023-06-09 17:24:38 +08:00
|
|
|
protected async Task<TValue?> GetAsync(TLookup lookup, CancellationToken token = default)
|
2020-11-06 12:50:51 +08:00
|
|
|
{
|
2023-06-09 17:24:38 +08:00
|
|
|
if (CheckExists(lookup, out TValue? existing))
|
2022-07-06 17:38:39 +08:00
|
|
|
{
|
|
|
|
statistics.Value.HitCount++;
|
2023-06-09 17:24:38 +08:00
|
|
|
return existing;
|
2022-07-06 17:38:39 +08:00
|
|
|
}
|
2020-11-06 12:50:51 +08:00
|
|
|
|
2021-03-08 11:57:16 +08:00
|
|
|
var computed = await ComputeValueAsync(lookup, token).ConfigureAwait(false);
|
2020-11-06 12:50:51 +08:00
|
|
|
|
2022-07-06 17:38:39 +08:00
|
|
|
statistics.Value.MissCount++;
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
if (computed != null || CacheNullValues)
|
2022-07-06 17:38:39 +08:00
|
|
|
{
|
2020-11-06 12:50:51 +08:00
|
|
|
cache[lookup] = computed;
|
2022-07-06 17:38:39 +08:00
|
|
|
statistics.Value.Usage = cache.Count;
|
|
|
|
}
|
2020-11-06 12:50:51 +08:00
|
|
|
|
|
|
|
return computed;
|
|
|
|
}
|
|
|
|
|
2022-06-30 16:17:06 +08:00
|
|
|
/// <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)
|
2022-06-24 17:39:44 +08:00
|
|
|
{
|
|
|
|
foreach (var kvp in cache)
|
|
|
|
{
|
2022-06-30 16:17:06 +08:00
|
|
|
if (matchKeyPredicate(kvp.Key))
|
2022-06-24 17:39:44 +08:00
|
|
|
cache.TryRemove(kvp.Key, out _);
|
|
|
|
}
|
2022-07-06 17:38:39 +08:00
|
|
|
|
|
|
|
statistics.Value.Usage = cache.Count;
|
2022-06-24 17:39:44 +08:00
|
|
|
}
|
|
|
|
|
2023-06-09 17:24:38 +08:00
|
|
|
protected bool CheckExists(TLookup lookup, [MaybeNullWhen(false)] out TValue value) =>
|
2020-11-06 13:31:21 +08:00
|
|
|
cache.TryGetValue(lookup, out value);
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
/// <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>
|
2023-06-09 17:24:38 +08:00
|
|
|
protected abstract Task<TValue?> ComputeValueAsync(TLookup lookup, CancellationToken token = default);
|
2022-07-06 17:38:39 +08:00
|
|
|
|
|
|
|
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%}";
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 12:26:18 +08:00
|
|
|
}
|
|
|
|
}
|