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

36 lines
1.2 KiB
C#
Raw Normal View History

2021-07-21 15:29:19 +05:00
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
2021-04-23 10:21:25 +05:00
using System.Collections.Generic;
2021-04-07 18:01:56 +05:00
namespace AsbCloudInfrastructure.Services.Cache
{
public class CacheDb
2021-04-23 10:21:25 +05:00
{
private readonly ConcurrentDictionary<string, CacheTableDataStore> cache =
new ConcurrentDictionary<string, CacheTableDataStore>();
2021-04-07 18:01:56 +05:00
2021-12-21 11:52:53 +05:00
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, params string[] includes)
where TEntity : class
=> GetCachedTable<TEntity>(context, includes);
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, IEnumerable<string> includes = null)
2021-04-07 18:01:56 +05:00
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);
2021-04-07 18:01:56 +05:00
return tableCache;
}
2021-04-23 10:21:25 +05:00
public void DropAll() => cache.Clear();
public void Drop<TEntity>() => cache.Remove(typeof(TEntity).FullName, out _);
2021-04-07 18:01:56 +05:00
}
}