From 6a4b426aa2ba860a7e8f778ce516941be8585d63 Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Wed, 29 Sep 2021 10:26:25 +0500 Subject: [PATCH] Changed CacheTable internal List to ConcurrentBag --- AsbCloudDbDemoData/ControllerLoadTester.cs | 5 +- .../Services/Cache/CacheDb.cs | 6 +- .../Services/Cache/CacheTable.cs | 127 +++++++++--------- 3 files changed, 68 insertions(+), 70 deletions(-) diff --git a/AsbCloudDbDemoData/ControllerLoadTester.cs b/AsbCloudDbDemoData/ControllerLoadTester.cs index 35ddafee..793efbaa 100644 --- a/AsbCloudDbDemoData/ControllerLoadTester.cs +++ b/AsbCloudDbDemoData/ControllerLoadTester.cs @@ -17,7 +17,10 @@ namespace AsbCloudDevOperations { var host = "http://localhost:5000"; - var json = JsonSerializer.Serialize(new List()); + var json = JsonSerializer.Serialize(new List() + { + new TelemetryDataSaubDto() + }); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); diff --git a/AsbCloudInfrastructure/Services/Cache/CacheDb.cs b/AsbCloudInfrastructure/Services/Cache/CacheDb.cs index 9afe92b6..a6c5baec 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheDb.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheDb.cs @@ -8,7 +8,9 @@ namespace AsbCloudInfrastructure.Services.Cache public class CacheDb { - private readonly ConcurrentDictionary)> cache = new ConcurrentDictionary)>(); + private readonly ConcurrentDictionary cache = + new ConcurrentDictionary(); + private readonly TimeSpan obsolesenceTime = TimeSpan.FromMinutes(15); public CacheTable GetCachedTable(DbContext context) @@ -17,7 +19,7 @@ namespace AsbCloudInfrastructure.Services.Cache var entityTypeName = typeof(TEntity).FullName; if (!cache.ContainsKey(entityTypeName)) - cache[entityTypeName] = (DateTime.Now, new List(8)); + cache[entityTypeName] = (DateTime.Now, new ConcurrentBag()); bool isCachedDataObsolete = DateTime.Now - cache[entityTypeName].Item1 > obsolesenceTime; diff --git a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs index a17a455c..e3155e70 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -11,15 +12,15 @@ namespace AsbCloudInfrastructure.Services.Cache where TEntity : class { private readonly DbContext context; - private (DateTime refreshDate, IEnumerable entities) data; - private readonly List cached; + private (DateTime refreshDate, object entities) data; + private readonly ConcurrentBag cached; private readonly DbSet dbSet; - internal CacheTable(DbContext context, (DateTime refreshDate, IEnumerable entities) data) + internal CacheTable(DbContext context, (DateTime refreshDate, object entities) data) { this.context = context; this.data = data; - this.cached = (List)data.entities; + this.cached = (ConcurrentBag)data.entities; dbSet = context.Set(); } @@ -27,41 +28,24 @@ namespace AsbCloudInfrastructure.Services.Cache public int Refresh() { - if (cached.Any()) - { - try - { - cached.Clear(); - } - catch - { - // ignore - // TODO: figure out what the hell - } + cached.Clear(); - } var dbEntities = context.Set().AsNoTracking().ToList(); - cached.AddRange(dbEntities); + foreach(var e in dbEntities) + cached.Add(e); data.refreshDate = DateTime.Now; return cached.Count; } public async Task RefreshAsync(CancellationToken token = default) { - if (cached.Any()) - { - try - { - cached.Clear(); - } - catch - { - // ignore - // TODO: figure out what the well - } - } - var dbEntities = await context.Set().AsNoTracking().ToListAsync(token).ConfigureAwait(false); - cached.AddRange(dbEntities); + cached.Clear(); + + var dbEntities = await context.Set().AsNoTracking() + .ToListAsync(token).ConfigureAwait(false); + + foreach (var e in dbEntities) + cached.Add(e); data.refreshDate = DateTime.Now; return cached.Count; } @@ -202,33 +186,40 @@ namespace AsbCloudInfrastructure.Services.Cache return result; } - public IEnumerable Mutate(Func predicate, Action mutation) - { - var dbEntities = dbSet.Where(predicate); - if (dbEntities.Any()) - { - foreach (var dbEntity in dbEntities) - mutation(dbEntity); - context.SaveChanges(); - } - cached.RemoveAll(e => predicate(e)); - cached.AddRange(dbEntities); - return dbEntities; - } + //public IEnumerable Mutate(Func predicate, + // Action mutation) + //{ + // var dbEntities = dbSet.Where(predicate); + // if (dbEntities.Any()) + // { + // foreach (var dbEntity in dbEntities) + // mutation(dbEntity); + // context.SaveChanges(); + // } + // var matchedByPredicate = cached.Select(el => predicate(el)); + // foreach (var item in matchedByPredicate) + // cached.TryTake(out var t); + // cached = cached.RemoveAll(e => predicate(e)); + // foreach (var e in dbEntities) + // cached.Add(e); + // return dbEntities; + //} - public async Task> MutateAsync(Func predicate, Action mutation, CancellationToken token = default) - { - var dbEntities = dbSet.Where(predicate); - if (dbEntities.Any()) - { - foreach (var dbEntity in dbEntities) - mutation(dbEntity); - await context.SaveChangesAsync(token).ConfigureAwait(false); - } - cached.RemoveAll(e => predicate(e)); - cached.AddRange(dbEntities); - return dbEntities; - } + //public async Task> MutateAsync(Func predicate, Action mutation, CancellationToken token = default) + //{ + // var dbEntities = dbSet.Where(predicate); + // if (dbEntities.Any()) + // { + // foreach (var dbEntity in dbEntities) + // mutation(dbEntity); + // await context.SaveChangesAsync(token).ConfigureAwait(false); + // } + // cached.RemoveAll(e => predicate(e)); + // foreach (var e in dbEntities) + // cached.Add(e); + // return dbEntities; + //} public TEntity Upsert(TEntity entity) { @@ -286,34 +277,34 @@ namespace AsbCloudInfrastructure.Services.Cache public void Remove(Func predicate) { - cached.RemoveAll(e => predicate(e)); dbSet.RemoveRange(dbSet.Where(predicate)); context.SaveChanges(); + Refresh(); return; } public async Task RemoveAsync(Func predicate, CancellationToken token = default) { - cached.RemoveAll(e => predicate(e)); dbSet.RemoveRange(dbSet.Where(predicate)); await context.SaveChangesAsync(token).ConfigureAwait(false); + await RefreshAsync(token).ConfigureAwait(false); return; } public TEntity Insert(TEntity entity) { - var dbEntity = dbSet.Add(entity).Entity; + var entry = dbSet.Add(entity); context.SaveChanges(); - cached.Add(dbEntity); - return dbEntity; + cached.Add(entry.Entity); + return entry.Entity; } public async Task InsertAsync(TEntity entity, CancellationToken token = default) { - var dbEntity = dbSet.Add(entity).Entity; + var entry = dbSet.Add(entity); await context.SaveChangesAsync(token).ConfigureAwait(false); - cached.Add(dbEntity); - return dbEntity; + cached.Add(entry.Entity); + return entry.Entity; } public IEnumerable Insert(IEnumerable newEntities) @@ -322,7 +313,8 @@ namespace AsbCloudInfrastructure.Services.Cache foreach (var item in newEntities) dbEntities.Add(dbSet.Add(item).Entity); context.SaveChanges(); - cached.AddRange(dbEntities); + foreach (var e in dbEntities) + cached.Add(e); return dbEntities; } @@ -332,7 +324,8 @@ namespace AsbCloudInfrastructure.Services.Cache foreach (var item in newEntities) dbEntities.Add(dbSet.Add(item).Entity); await context.SaveChangesAsync(token).ConfigureAwait(false); - cached.AddRange(dbEntities); + foreach (var e in dbEntities) + cached.Add(e); return dbEntities; }