2021-07-21 15:29:19 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using System;
|
2021-04-09 17:59:07 +05:00
|
|
|
|
using System.Collections.Concurrent;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
using System.Collections.Generic;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using System.Linq;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.Cache
|
|
|
|
|
{
|
|
|
|
|
public class CacheDb
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
|
private readonly ConcurrentDictionary<string, CacheTableDataStore> cache =
|
2021-11-11 10:57:08 +05:00
|
|
|
|
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
|
2021-12-22 11:40:29 +05:00
|
|
|
|
=> GetCachedTable<TEntity>(context, new SortedSet<string>(includes));
|
2021-12-21 11:52:53 +05:00
|
|
|
|
|
2021-12-22 11:40:29 +05:00
|
|
|
|
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, ISet<string> includes = null)
|
|
|
|
|
where TEntity : class
|
|
|
|
|
{
|
|
|
|
|
var cacheItem = GetCacheTableDataStore<TEntity>();
|
|
|
|
|
var tableCache = new CacheTable<TEntity>(context, cacheItem, includes);
|
|
|
|
|
return tableCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> configureDbSet)
|
|
|
|
|
where TEntity : class
|
|
|
|
|
{
|
|
|
|
|
var cacheItem = GetCacheTableDataStore<TEntity>();
|
|
|
|
|
var tableCache = new CacheTable<TEntity>(context, cacheItem, configureDbSet);
|
|
|
|
|
return tableCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CacheTableDataStore GetCacheTableDataStore<TEntity>()
|
2021-04-07 18:01:56 +05:00
|
|
|
|
where TEntity : class
|
|
|
|
|
{
|
2021-11-11 10:57:08 +05:00
|
|
|
|
var nameOfTEntity = typeof(TEntity).FullName;
|
2021-12-22 11:40:29 +05:00
|
|
|
|
var cacheItem = cache.GetOrAdd(nameOfTEntity, (nameOfTEntity) => new CacheTableDataStore
|
|
|
|
|
{
|
2021-11-11 10:57:08 +05:00
|
|
|
|
NameOfTEntity = nameOfTEntity,
|
|
|
|
|
Entities = new List<TEntity>(),
|
|
|
|
|
});
|
2021-12-22 11:40:29 +05:00
|
|
|
|
return cacheItem;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|