diff --git a/AsbCloudInfrastructure/Services/Cache/CacheDb.cs b/AsbCloudInfrastructure/Services/Cache/CacheDb.cs index e4c8cf8f..a43be14b 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheDb.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheDb.cs @@ -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 cache = new ConcurrentDictionary(); - public CacheTable GetCachedTable(DbContext context, params string[] includes) where TEntity : class - => GetCachedTable(context, includes); + => GetCachedTable(context, new SortedSet(includes)); - public CacheTable GetCachedTable(DbContext context, IEnumerable includes = null) + public CacheTable GetCachedTable(DbContext context, ISet includes = null) + where TEntity : class + { + var cacheItem = GetCacheTableDataStore(); + var tableCache = new CacheTable(context, cacheItem, includes); + return tableCache; + } + + public CacheTable GetCachedTable(DbContext context, Func, IQueryable> configureDbSet) + where TEntity : class + { + var cacheItem = GetCacheTableDataStore(); + var tableCache = new CacheTable(context, cacheItem, configureDbSet); + return tableCache; + } + + private CacheTableDataStore GetCacheTableDataStore() 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(), }); - var tableCache = new CacheTable(context, cacheItem, includes); - return tableCache; + return cacheItem; } public void DropAll() => cache.Clear(); diff --git a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs index 2ff98b5e..b1932283 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs @@ -23,22 +23,21 @@ namespace AsbCloudInfrastructure.Services.Cache private readonly DbContext context; private readonly DbSet dbSet; - internal CacheTable(DbContext context, CacheTableDataStore data, IEnumerable includes = null) + internal CacheTable(DbContext context, CacheTableDataStore data, ISet includes = null) { this.context = context; this.data = data; dbSet = context.Set(); - + if (includes?.Any() == true) configureDbSet = (DbSet dbSet) => { IQueryable result = dbSet; - foreach (var include in includes) - result = result.Include(include); + foreach (var include in includes) + result = result.Include(include); return result; }; - cached = (List)data.Entities; if ((cached.Count == 0) || data.IsObsolete) Refresh(false); diff --git a/AsbCloudInfrastructure/Services/Cache/CacheTableDataStore.cs b/AsbCloudInfrastructure/Services/Cache/CacheTableDataStore.cs index be9dfcf8..ef900b11 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheTableDataStore.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheTableDataStore.cs @@ -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 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);