fix CacheDb. Make cache ConcurrentDictionary

This commit is contained in:
Фролов 2021-10-18 11:06:42 +05:00
parent 589959c802
commit 0a3891a332

View File

@ -9,8 +9,8 @@ namespace AsbCloudInfrastructure.Services.Cache
public class CacheDb public class CacheDb
{ {
private readonly Dictionary<string, (DateTime, IEnumerable)> cache = private readonly ConcurrentDictionary<string, (DateTime, IEnumerable)> cache =
new Dictionary<string, (DateTime, IEnumerable)>(); new ConcurrentDictionary<string, (DateTime, IEnumerable)>();
private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15); private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15);
@ -19,12 +19,11 @@ namespace AsbCloudInfrastructure.Services.Cache
{ {
var entityTypeName = typeof(TEntity).FullName; var entityTypeName = typeof(TEntity).FullName;
if (!cache.ContainsKey(entityTypeName)) var cacheItem = cache.GetOrAdd(entityTypeName, (DateTime.Now, new List<TEntity>()));
cache[entityTypeName] = (DateTime.Now, new List<TEntity>());
bool isCachedDataObsolete = DateTime.Now - cache[entityTypeName].Item1 > obsolesenceTime; bool isCachedDataObsolete = DateTime.Now - cacheItem.Item1 > obsolesenceTime;
var tableCache = new CacheTable<TEntity>(context, cache[entityTypeName]); var tableCache = new CacheTable<TEntity>(context, cacheItem);
if (isCachedDataObsolete) if (isCachedDataObsolete)
tableCache.Refresh(); tableCache.Refresh();