using System; using System.Collections.Concurrent; namespace CryptoExchange.Net.Caching { internal class MemoryCache { private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); /// /// Add a new cache entry. Will override an existing entry if it already exists /// /// The key identifier /// Cache value public void Add(string key, object value) { var cacheItem = new CacheItem(DateTime.UtcNow, value); _cache.AddOrUpdate(key, cacheItem, (key, val1) => cacheItem); } /// /// Get a cached value /// /// The key identifier /// The max age of the cached entry /// Cached value if it was in cache public object? Get(string key, TimeSpan maxAge) { _cache.TryGetValue(key, out CacheItem value); if (value == null) return null; if (DateTime.UtcNow - value.CacheTime > maxAge) { _cache.TryRemove(key, out _); return null; } return value.Value; } private class CacheItem { public DateTime CacheTime { get; } public object Value { get; } public CacheItem(DateTime cacheTime, object value) { CacheTime = cacheTime; Value = value; } } } }