forked from ddrilling/AsbCloudServer
refactor CacheTable. Includes IEnumerable replaced to ISet [unique values collection]
This commit is contained in:
parent
40145f7d5b
commit
a9f03c2ecf
@ -1,31 +1,47 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System.Linq;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System;
|
||||||
|
using AsbCloudApp;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services.Cache
|
namespace AsbCloudInfrastructure.Services.Cache
|
||||||
{
|
{
|
||||||
|
|
||||||
public class CacheDb
|
public class CacheDb
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<string, CacheTableDataStore> cache =
|
private readonly ConcurrentDictionary<string, CacheTableDataStore> cache =
|
||||||
new ConcurrentDictionary<string, CacheTableDataStore>();
|
new ConcurrentDictionary<string, CacheTableDataStore>();
|
||||||
|
|
||||||
|
|
||||||
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, params string[] includes)
|
public CacheTable<TEntity> GetCachedTable<TEntity>(DbContext context, params string[] includes)
|
||||||
where TEntity : class
|
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
|
where TEntity : class
|
||||||
{
|
{
|
||||||
var nameOfTEntity = typeof(TEntity).FullName;
|
var nameOfTEntity = typeof(TEntity).FullName;
|
||||||
var cacheItem = cache.GetOrAdd(nameOfTEntity, (nameOfTEntity) => new CacheTableDataStore {
|
var cacheItem = cache.GetOrAdd(nameOfTEntity, (nameOfTEntity) => new CacheTableDataStore
|
||||||
|
{
|
||||||
NameOfTEntity = nameOfTEntity,
|
NameOfTEntity = nameOfTEntity,
|
||||||
Entities = new List<TEntity>(),
|
Entities = new List<TEntity>(),
|
||||||
});
|
});
|
||||||
var tableCache = new CacheTable<TEntity>(context, cacheItem, includes);
|
return cacheItem;
|
||||||
return tableCache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DropAll() => cache.Clear();
|
public void DropAll() => cache.Clear();
|
||||||
|
@ -23,7 +23,7 @@ namespace AsbCloudInfrastructure.Services.Cache
|
|||||||
private readonly DbContext context;
|
private readonly DbContext context;
|
||||||
private readonly DbSet<TEntity> dbSet;
|
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.context = context;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
@ -38,7 +38,6 @@ namespace AsbCloudInfrastructure.Services.Cache
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
cached = (List<TEntity>)data.Entities;
|
cached = (List<TEntity>)data.Entities;
|
||||||
if ((cached.Count == 0) || data.IsObsolete)
|
if ((cached.Count == 0) || data.IsObsolete)
|
||||||
Refresh(false);
|
Refresh(false);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services.Cache
|
namespace AsbCloudInfrastructure.Services.Cache
|
||||||
{
|
{
|
||||||
@ -7,6 +8,8 @@ namespace AsbCloudInfrastructure.Services.Cache
|
|||||||
{
|
{
|
||||||
public string NameOfTEntity { get; set; }
|
public string NameOfTEntity { get; set; }
|
||||||
public DateTime LastResreshDate { 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 IEnumerable Entities { get; set; }
|
||||||
public TimeSpan ObsolesenceTime { get; set; } = TimeSpan.FromMinutes(15);
|
public TimeSpan ObsolesenceTime { get; set; } = TimeSpan.FromMinutes(15);
|
||||||
public bool IsObsolete => (DateTime.Now - LastResreshDate > ObsolesenceTime);
|
public bool IsObsolete => (DateTime.Now - LastResreshDate > ObsolesenceTime);
|
||||||
|
Loading…
Reference in New Issue
Block a user