DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Cache/CacheDb.cs
2021-04-23 10:21:25 +05:00

29 lines
919 B
C#

using Microsoft.EntityFrameworkCore;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services.Cache
{
public class CacheDb
{
private readonly ConcurrentDictionary<string, IEnumerable<object>> cache = new ConcurrentDictionary<string, IEnumerable<object>>();
public ICacheTable<TEntity> GetCachedTable<TEntity>(DbContext context)
where TEntity : class
{
var entityTypeName = typeof(TEntity).FullName;
if (!cache.ContainsKey(entityTypeName))
cache[entityTypeName] = new List<TEntity>(8);
var tableCache = new CacheTable<TEntity>(context, (List<TEntity>)cache[entityTypeName]);
return tableCache;
}
public void DropAll() => cache.Clear();
public void Drop<TEntity>() => cache.Remove(typeof(TEntity).FullName, out _);
}
}