using System; using System.Collections.Concurrent; namespace PkmnLibSharp.Utils { internal static class CacheHandler { private static readonly ConcurrentDictionary Caches = new ConcurrentDictionary(); internal static T GetCache(IntPtr ptr, Func 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 _); } } }