using Microsoft.Extensions.Caching.Memory; namespace KonSoft.InternalGateway.Aggregations.Base; public abstract class CachedServiceBase : ICachedServiceBase { private readonly IMemoryCache _cache; protected MemoryCacheEntryOptions CacheEntryOptions { get; } = new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24), SlidingExpiration = TimeSpan.FromHours(4) }; protected CachedServiceBase(IMemoryCache cache) { _cache = cache ?? throw new ArgumentNullException(nameof(cache)); } public void Add(string serviceName, TCacheValue data) { _cache.Set(serviceName, data, CacheEntryOptions); } public IDictionary GetManyAsync(IEnumerable serviceNames) { var result = new Dictionary(); foreach (var serviceName in serviceNames) { if (_cache.TryGetValue(serviceName, out TCacheValue data)) { result.Add(serviceName, data); } } return result; } }