diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 2fa7b639..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,13 +110,26 @@ public class WellOperationRepository : CrudRepositoryBase> GetSectionsAsync(IEnumerable idsWells, CancellationToken token) { - const string keyCacheSections = "OperationsBySectionSummarties"; + var result = new List(); + var notFoundIds = new List(); - var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) => + foreach (var idWell in idsWells) { - entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); + var cacheKey = string.Format(keyCacheTemplate, idWell); + if (memoryCache.TryGetValue>(cacheKey, out var section)) + { + result.AddRange(section!); + } + else + { + notFoundIds.Add(idWell); + } + } + if (notFoundIds.Count != 0) + { var query = dbContext.Set() + .Where(operation => notFoundIds.Contains( operation.IdWell)) .GroupBy(operation => new { operation.IdWell, @@ -129,51 +143,49 @@ public class WellOperationRepository : CrudRepositoryBase operation.DateStart) - .Select(operation => new - { - operation.DateStart, - operation.DepthStart, - }) - .First(), - + .OrderBy(operation => operation.DateStart) + .Select(operation => new { operation.DateStart, operation.DepthStart, }) + .First(), Last = group - .OrderByDescending(operation => operation.DateStart) - .Select(operation => new - { - operation.DateStart, - operation.DurationHours, - operation.DepthEnd, - }) - .First(), - }) - .Where(s => idsWells.Contains(s.IdWell)); - var dbData = await query.ToArrayAsync(token); - var sections = dbData.Select( - item => new SectionByOperationsDto + .OrderByDescending(operation => operation.DateStart) + .Select(operation => new + { + operation.DateStart, + operation.DurationHours, + operation.DepthEnd, + }) + .First(), + }); + + var entities = await query.ToArrayAsync(token); + var dtos = entities.Select( + entity => new SectionByOperationsDto { - IdWell = item.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, }) - .ToArray() - .AsEnumerable(); + .ToList(); - entry.Value = sections; - return sections; - }); + result.AddRange(dtos); - return cache!; + var groupedByWellDtos = dtos + .GroupBy(dto => dto.IdWell); + + foreach (var group in groupedByWellDtos) + { + var cacheKey = string.Format(keyCacheTemplate, group.Key); + memoryCache.Set(cacheKey, group.AsEnumerable(), TimeSpan.FromMinutes(30)); + } + } + + return result; } public async Task GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)