From 9dd530a9fbe67ca39b6b9d47a5e7e3c3f2e86f38 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 4 Sep 2024 15:19:09 +0500 Subject: [PATCH] =?UTF-8?q?fix=20WellOperationRepository.GetSectionsAsync?= =?UTF-8?q?=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=B8=D0=B7=20=D0=91?= =?UTF-8?q?=D0=94=20=D0=B7=D0=B0=D0=BF=D1=80=D0=B0=D1=88=D0=B8=D0=B2=D0=B0?= =?UTF-8?q?=D1=8E=D1=82=D1=81=D1=8F=20=D0=BE=D0=B4=D0=BD=D0=B8=D0=BC=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repository/WellOperationRepository.cs | 91 +++++++++++-------- 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 3d485db9..92f5844c 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -20,6 +20,7 @@ namespace AsbCloudInfrastructure.Repository; public class WellOperationRepository : CrudRepositoryBase, IWellOperationRepository { + private const string keyCacheTemplate = "OperationsBySectionSummaries_{0}"; private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations"; private readonly IMemoryCache memoryCache; private readonly IWellOperationCategoryRepository wellOperationCategoryRepository; @@ -109,61 +110,79 @@ public class WellOperationRepository : CrudRepositoryBase> GetSectionsAsync(IEnumerable idsWells, CancellationToken token) { - const string keyCacheTemplate = "OperationsBySectionSummaries_{0}"; - var result = new List(); + var notFoundIds = new List(); foreach (var idWell in idsWells) { - var sections = await memoryCache.GetOrCreateAsync(string.Format(keyCacheTemplate, idWell), async (entry) => + var cacheKey = string.Format(keyCacheTemplate, idWell); + if (memoryCache.TryGetValue>(cacheKey, out var section)) { - entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); + result.AddRange(section!); + } + else + { + notFoundIds.Add(idWell); + } + } - var query = dbContext.Set() - .Where(x => x.IdWell == idWell) - .GroupBy(operation => new - { - operation.IdType, operation.IdWellSectionType, operation.WellSectionType.Caption, - }) - .Select(group => new - { - group.Key.IdType, - group.Key.IdWellSectionType, - group.Key.Caption, - First = group + if (notFoundIds.Count != 0) + { + var query = dbContext.Set() + .Where(operation => notFoundIds.Contains( operation.IdWell)) + .GroupBy(operation => new + { + operation.IdWell, + operation.IdType, + operation.IdWellSectionType, + operation.WellSectionType.Caption, + }) + .Select(group => new + { + group.Key.IdWell, + group.Key.IdType, + group.Key.IdWellSectionType, + group.Key.Caption, + First = group .OrderBy(operation => operation.DateStart) .Select(operation => new { operation.DateStart, operation.DepthStart, }) .First(), - Last = group + Last = group .OrderByDescending(operation => operation.DateStart) .Select(operation => new { - operation.DateStart, operation.DurationHours, operation.DepthEnd, + operation.DateStart, + operation.DurationHours, + operation.DepthEnd, }) .First(), - }); + }); - var dbData = await query.ToArrayAsync(token); - - var sections = dbData.Select( - item => new SectionByOperationsDto + var entities = await query.ToArrayAsync(token); + var dtos = entities.Select( + entity => new SectionByOperationsDto { - IdWell = idWell, - IdType = item.IdType, - IdWellSectionType = item.IdWellSectionType, - Caption = item.Caption, - DateStart = item.First.DateStart, - DepthStart = item.First.DepthStart, - DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours), - DepthEnd = item.Last.DepthEnd, - }); + IdWell = entity.IdWell, + IdType = entity.IdType, + IdWellSectionType = entity.IdWellSectionType, + Caption = entity.Caption, + DateStart = entity.First.DateStart, + DepthStart = entity.First.DepthStart, + DateEnd = entity.Last.DateStart.AddHours(entity.Last.DurationHours), + DepthEnd = entity.Last.DepthEnd, + }) + .ToList(); + result.AddRange(dtos); - entry.Value = sections; - return sections; - }); + var groupedByWellDtos = dtos + .GroupBy(dto => dto.IdWell); - result.AddRange(sections!); + foreach (var group in groupedByWellDtos) + { + var cacheKey = string.Format(keyCacheTemplate, group.Key); + memoryCache.Set(cacheKey, group.AsEnumerable(), TimeSpan.FromMinutes(30)); + } } return result;