using Microsoft.Extensions.Caching.Memory; namespace Harmonia.Core.Caching; public abstract class MemoryCache : Cache where TKey : notnull { private readonly IMemoryCache _memoryCache; protected abstract MemoryCacheOptions Options { get; } protected abstract TimeSpan SlidingExpiration { get; } public MemoryCache() { _memoryCache = new MemoryCache(Options); } protected override TValue? TryGetValue(object key) { _memoryCache.TryGetValue(key, out TValue? value); return value; } protected override void AddToCache(object key, TValue entry) { long entrySize = GetEntrySize(entry); var cacheEntryOptions = new MemoryCacheEntryOptions() .SetSize(entrySize) .SetSlidingExpiration(SlidingExpiration); _memoryCache.Set(key, entry, cacheEntryOptions); } protected abstract long GetEntrySize(TValue entry); }