2020-11-06 04:26:18 +00: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-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2022-06-24 09:39:44 +00:00
|
|
|
using System;
|
2020-11-06 04:26:18 +00:00
|
|
|
using System.Collections.Concurrent;
|
2020-11-06 04:50:51 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using JetBrains.Annotations;
|
2020-11-06 04:26:18 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
|
|
|
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 class MemoryCachingComponent<TLookup, TValue> : Component
|
|
|
|
{
|
2020-11-06 04:50:51 +00:00
|
|
|
private readonly ConcurrentDictionary<TLookup, TValue> cache = new ConcurrentDictionary<TLookup, TValue>();
|
|
|
|
|
|
|
|
protected virtual bool CacheNullValues => true;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-06 05:31:21 +00:00
|
|
|
/// Retrieve the cached value for the given lookup.
|
2020-11-06 04:50:51 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="lookup">The lookup to retrieve.</param>
|
|
|
|
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
|
|
|
|
protected async Task<TValue> GetAsync([NotNull] TLookup lookup, CancellationToken token = default)
|
|
|
|
{
|
2020-11-06 07:57:09 +00:00
|
|
|
if (CheckExists(lookup, out TValue performance))
|
2020-11-06 04:50:51 +00:00
|
|
|
return performance;
|
|
|
|
|
2021-03-08 03:57:16 +00:00
|
|
|
var computed = await ComputeValueAsync(lookup, token).ConfigureAwait(false);
|
2020-11-06 04:50:51 +00:00
|
|
|
|
|
|
|
if (computed != null || CacheNullValues)
|
|
|
|
cache[lookup] = computed;
|
|
|
|
|
|
|
|
return computed;
|
|
|
|
}
|
|
|
|
|
2022-06-24 09:39:44 +00:00
|
|
|
protected void Invalidate(Func<TLookup, bool> invalidationFunction)
|
|
|
|
{
|
|
|
|
foreach (var kvp in cache)
|
|
|
|
{
|
|
|
|
if (invalidationFunction(kvp.Key))
|
|
|
|
cache.TryRemove(kvp.Key, out _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:31:21 +00:00
|
|
|
protected bool CheckExists([NotNull] TLookup lookup, out TValue value) =>
|
|
|
|
cache.TryGetValue(lookup, out value);
|
|
|
|
|
2020-11-06 04:50:51 +00: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>
|
|
|
|
protected abstract Task<TValue> ComputeValueAsync(TLookup lookup, CancellationToken token = default);
|
2020-11-06 04:26:18 +00:00
|
|
|
}
|
|
|
|
}
|