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

32 lines
996 B
C#
Raw Normal View History

2021-04-07 18:01:56 +05:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
2021-04-07 18:01:56 +05:00
using System.Text;
namespace AsbCloudInfrastructure.Services.Cache
{
public class CacheDb
{
private ConcurrentDictionary<string, IEnumerable<object>> cache = new ConcurrentDictionary<string, IEnumerable<object>>();
2021-04-07 18:01:56 +05:00
public ICacheTable<TEntity> GetCachedTable<TEntity>(DbContext context)
where TEntity : class
{
var entityTypeName = typeof(TEntity).FullName;
2021-04-07 18:01:56 +05:00
if (!cache.ContainsKey(entityTypeName))
cache[entityTypeName] = new List<TEntity>(8);
var tableCache = new CacheTable<TEntity>(context, (List<TEntity>)cache[entityTypeName]);
2021-04-07 18:01:56 +05:00
return tableCache;
}
public void DropAll()=> cache.Clear();
public void Drop<TEntity>() => cache.Remove(typeof(TEntity).FullName, out _);
2021-04-07 18:01:56 +05:00
}
}