DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Cache/CacheDb.cs
2021-11-11 10:57:08 +05:00

30 lines
1001 B
C#

using Microsoft.EntityFrameworkCore;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services.Cache
{
public class CacheDb
{
private readonly ConcurrentDictionary<string, CacheTableDataStore> cache =
new ConcurrentDictionary<string, CacheTableDataStore>();
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context)
where TEntity : class
{
var nameOfTEntity = typeof(TEntity).FullName;
var cacheItem = cache.GetOrAdd(nameOfTEntity, (nameOfTEntity) => new CacheTableDataStore {
NameOfTEntity = nameOfTEntity,
Entities = new List<TEntity>(),
});
var tableCache = new CacheTable<TEntity>(context, cacheItem);
return tableCache;
}
public void DropAll() => cache.Clear();
public void Drop<TEntity>() => cache.Remove(typeof(TEntity).FullName, out _);
}
}