using Microsoft.EntityFrameworkCore; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; namespace AsbCloudInfrastructure.Services.Cache { public class CacheDb { private readonly ConcurrentDictionary cache = new ConcurrentDictionary(); private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15); public CacheTable GetCachedTable(DbContext context) where TEntity : class { var entityTypeName = typeof(TEntity).FullName; var cacheItem = cache.GetOrAdd(entityTypeName, (DateTime.Now, new List())); bool isCachedDataObsolete = DateTime.Now - cacheItem.Item1 > obsolesenceTime; var tableCache = new CacheTable(context, cacheItem); if (isCachedDataObsolete) tableCache.Refresh(); return tableCache; } public void DropAll() => cache.Clear(); public void Drop() => cache.Remove(typeof(TEntity).FullName, out _); } }