DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Cache/CacheDb.cs

31 lines
1.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using System;
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, IEnumerable<string> includes = null)
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, includes);
return tableCache;
}
public void DropAll() => cache.Clear();
public void Drop<TEntity>() => cache.Remove(typeof(TEntity).FullName, out _);
}
}