31 lines
848 B
C#
31 lines
848 B
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace PkmnLibSharp.Utils
|
|
{
|
|
internal static class CacheHandler
|
|
{
|
|
private static readonly ConcurrentDictionary<ulong, object> Caches = new();
|
|
|
|
internal static T GetCache<T>(ulong id, Func<object> ctor)
|
|
{
|
|
if (!Caches.TryGetValue(id, out var cache))
|
|
{
|
|
cache = ctor();
|
|
Caches.TryAdd(id, cache);
|
|
}
|
|
|
|
if (cache.GetType() != typeof(T))
|
|
{
|
|
throw new InvalidCastException(
|
|
$"Can't cast from cache data `{cache.GetType().FullName}` to cache data `{typeof(T).FullName}`");
|
|
}
|
|
return (T)cache;
|
|
}
|
|
|
|
internal static void RemoveCache(ulong id)
|
|
{
|
|
Caches.TryRemove(id, out _);
|
|
}
|
|
}
|
|
} |