refactor CacheTable. Includes IEnumerable replaced to ISet [unique values collection]

This commit is contained in:
Фролов 2021-12-22 11:40:29 +05:00
parent 40145f7d5b
commit a9f03c2ecf
3 changed files with 31 additions and 13 deletions

View File

@ -1,31 +1,47 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System;
using AsbCloudApp;
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);
=> GetCachedTable<TEntity>(context, new SortedSet<string>(includes));
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, IEnumerable<string> includes = null)
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>()
where TEntity : class
{
var nameOfTEntity = typeof(TEntity).FullName;
var cacheItem = cache.GetOrAdd(nameOfTEntity, (nameOfTEntity) => new CacheTableDataStore {
var cacheItem = cache.GetOrAdd(nameOfTEntity, (nameOfTEntity) => new CacheTableDataStore
{
NameOfTEntity = nameOfTEntity,
Entities = new List<TEntity>(),
});
var tableCache = new CacheTable<TEntity>(context, cacheItem, includes);
return tableCache;
return cacheItem;
}
public void DropAll() => cache.Clear();

View File

@ -23,22 +23,21 @@ namespace AsbCloudInfrastructure.Services.Cache
private readonly DbContext context;
private readonly DbSet<TEntity> dbSet;
internal CacheTable(DbContext context, CacheTableDataStore data, IEnumerable<string> includes = null)
internal CacheTable(DbContext context, CacheTableDataStore data, ISet<string> includes = null)
{
this.context = context;
this.data = data;
dbSet = context.Set<TEntity>();
if (includes?.Any() == true)
configureDbSet = (DbSet<TEntity> dbSet) =>
{
IQueryable<TEntity> result = dbSet;
foreach (var include in includes)
result = result.Include(include);
foreach (var include in includes)
result = result.Include(include);
return result;
};
cached = (List<TEntity>)data.Entities;
if ((cached.Count == 0) || data.IsObsolete)
Refresh(false);

View File

@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services.Cache
{
@ -7,6 +8,8 @@ namespace AsbCloudInfrastructure.Services.Cache
{
public string NameOfTEntity { get; set; }
public DateTime LastResreshDate { get; set; }
//public ISet<string> Includes { get; set; } //TODO: this prop change should update entities
public IEnumerable Entities { get; set; }
public TimeSpan ObsolesenceTime { get; set; } = TimeSpan.FromMinutes(15);
public bool IsObsolete => (DateTime.Now - LastResreshDate > ObsolesenceTime);