PkmnLibRSharp/PkmnLibRSharp/Utils/CacheHandler.cs

25 lines
652 B
C#

using System;
using System.Collections.Concurrent;
namespace PkmnLibSharp.Utils
{
internal static class CacheHandler
{
private static readonly ConcurrentDictionary<IntPtr, object> Caches = new ConcurrentDictionary<IntPtr, object>();
internal static T GetCache<T>(IntPtr ptr, Func<object> ctor)
{
if (!Caches.TryGetValue(ptr, out var cache))
{
cache = ctor();
Caches.TryAdd(ptr, cache);
}
return (T)cache;
}
internal static void RemoveCache(IntPtr ptr)
{
Caches.TryRemove(ptr, out _);
}
}
}