forked from ddrilling/AsbCloudServer
36 lines
1.2 KiB
C#
36 lines
1.2 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, params string[] includes)
|
|
where TEntity : class
|
|
=> GetCachedTable<TEntity>(context, includes);
|
|
|
|
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 _);
|
|
}
|
|
}
|