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 Dictionary cache = new Dictionary(); private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15); public CacheTable GetCachedTable(DbContext context) where TEntity : class { var entityTypeName = typeof(TEntity).FullName; if (!cache.ContainsKey(entityTypeName)) cache[entityTypeName] = (DateTime.Now, new List()); bool isCachedDataObsolete = DateTime.Now - cache[entityTypeName].Item1 > obsolesenceTime; var tableCache = new CacheTable(context, cache[entityTypeName]); if (isCachedDataObsolete) tableCache.Refresh(); return tableCache; } public void DropAll() => cache.Clear(); public void Drop() => cache.Remove(typeof(TEntity).FullName, out _); } }