From 0a3891a33233b01799ba06be8568ad94beb48570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 18 Oct 2021 11:06:42 +0500 Subject: [PATCH] fix CacheDb. Make cache ConcurrentDictionary --- AsbCloudInfrastructure/Services/Cache/CacheDb.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Cache/CacheDb.cs b/AsbCloudInfrastructure/Services/Cache/CacheDb.cs index dcd27a58..ea84bd9e 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheDb.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheDb.cs @@ -9,8 +9,8 @@ namespace AsbCloudInfrastructure.Services.Cache public class CacheDb { - private readonly Dictionary cache = - new Dictionary(); + private readonly ConcurrentDictionary cache = + new ConcurrentDictionary(); private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15); @@ -19,12 +19,11 @@ namespace AsbCloudInfrastructure.Services.Cache { var entityTypeName = typeof(TEntity).FullName; - if (!cache.ContainsKey(entityTypeName)) - cache[entityTypeName] = (DateTime.Now, new List()); + var cacheItem = cache.GetOrAdd(entityTypeName, (DateTime.Now, new List())); - bool isCachedDataObsolete = DateTime.Now - cache[entityTypeName].Item1 > obsolesenceTime; + bool isCachedDataObsolete = DateTime.Now - cacheItem.Item1 > obsolesenceTime; - var tableCache = new CacheTable(context, cache[entityTypeName]); + var tableCache = new CacheTable(context, cacheItem); if (isCachedDataObsolete) tableCache.Refresh();