2021-04-07 18:01:56 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-04-09 17:59:07 +05:00
|
|
|
|
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, IEnumerable<object>> cache = new ConcurrentDictionary<string, IEnumerable<object>>();
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-05-20 14:14:51 +05:00
|
|
|
|
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context)
|
2021-04-07 18:01:56 +05:00
|
|
|
|
where TEntity : class
|
|
|
|
|
{
|
2021-04-09 17:59:07 +05:00
|
|
|
|
var entityTypeName = typeof(TEntity).FullName;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-04-09 17:59:07 +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;
|
|
|
|
|
}
|
2021-04-09 17:59:07 +05:00
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
public void DropAll() => cache.Clear();
|
2021-04-09 17:59:07 +05:00
|
|
|
|
|
|
|
|
|
public void Drop<TEntity>() => cache.Remove(typeof(TEntity).FullName, out _);
|
2021-04-07 18:01:56 +05:00
|
|
|
|
}
|
|
|
|
|
}
|