forked from ddrilling/AsbCloudServer
32 lines
996 B
C#
32 lines
996 B
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using System.Text;
|
|
|
|
namespace AsbCloudInfrastructure.Services.Cache
|
|
{
|
|
|
|
public class CacheDb
|
|
{
|
|
private 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 _);
|
|
}
|
|
}
|