using System.Collections; using System.Collections.Concurrent; using System.Reactive.Disposables; namespace Toolkit.Foundation; public class Cache(IDisposer disposer, IComparer comparer) : ICache { private readonly SortedSet cache = new(comparer); public void Add(TValue value) { if (value is null) { return; } disposer.Add(value, Disposable.Create(() => Remove(value))); cache.Add(value); } public void Clear() => cache.Clear(); public IEnumerator GetEnumerator() => cache.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public bool Remove(TValue value) => cache.Remove(value); } public class Cache : ICache where TKey : notnull where TValue : notnull { private readonly ConcurrentDictionary cache = new(); public void Add(TKey key, TValue value) { cache.TryAdd(key, value); } public void Clear() => cache.Clear(); public bool ContainsKey(TKey key) => cache.ContainsKey(key); public IEnumerator> GetEnumerator() => cache.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public bool Remove(TKey key) => cache.Remove(key, out _); public bool TryGetValue(TKey key, out TValue? value) { if (cache.TryGetValue(key, out value)) { return true; } value = default; return false; } }