Добавление записей в кэш для каждой скважины

This commit is contained in:
Степанов Дмитрий 2024-09-04 14:06:28 +05:00
parent 787fd478eb
commit aa5c67d2e7

View File

@ -109,71 +109,64 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token) public async Task<IEnumerable<SectionByOperationsDto>> GetSectionsAsync(IEnumerable<int> idsWells, CancellationToken token)
{ {
var keyCacheSections = $"OperationsBySectionSummaries_{string.Join('_', idsWells.OrderBy(x => x))}"; const string keyCacheTemplate = "OperationsBySectionSummaries_{0}";
var cache = await memoryCache.GetOrCreateAsync(keyCacheSections, async (entry) => var result = new List<SectionByOperationsDto>();
foreach (var idWell in idsWells)
{
var sections = await memoryCache.GetOrCreateAsync(string.Format(keyCacheTemplate, idWell), async (entry) =>
{ {
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30); entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
var query = dbContext.Set<WellOperation>() var query = dbContext.Set<WellOperation>()
.Where(x => x.IdWell == idWell)
.GroupBy(operation => new .GroupBy(operation => new
{ {
operation.IdWell, operation.IdType, operation.IdWellSectionType, operation.WellSectionType.Caption,
operation.IdType,
operation.IdWellSectionType,
operation.WellSectionType.Caption,
}) })
.Select(group => new .Select(group => new
{ {
group.Key.IdWell,
group.Key.IdType, group.Key.IdType,
group.Key.IdWellSectionType, group.Key.IdWellSectionType,
group.Key.Caption, group.Key.Caption,
First = group First = group
.OrderBy(operation => operation.DateStart) .OrderBy(operation => operation.DateStart)
.Select(operation => new .Select(operation => new { operation.DateStart, operation.DepthStart, })
{
operation.DateStart,
operation.DepthStart,
})
.First(), .First(),
Last = group Last = group
.OrderByDescending(operation => operation.DateStart) .OrderByDescending(operation => operation.DateStart)
.Select(operation => new .Select(operation => new
{ {
operation.DateStart, operation.DateStart, operation.DurationHours, operation.DepthEnd,
operation.DurationHours,
operation.DepthEnd,
}) })
.First(), .First(),
}) });
.Where(s => idsWells.Contains(s.IdWell));
var dbData = await query.ToArrayAsync(token); var dbData = await query.ToArrayAsync(token);
var sections = dbData.Select( var sections = dbData.Select(
item => new SectionByOperationsDto item => new SectionByOperationsDto
{ {
IdWell = item.IdWell, IdWell = idWell,
IdType = item.IdType, IdType = item.IdType,
IdWellSectionType = item.IdWellSectionType, IdWellSectionType = item.IdWellSectionType,
Caption = item.Caption, Caption = item.Caption,
DateStart = item.First.DateStart, DateStart = item.First.DateStart,
DepthStart = item.First.DepthStart, DepthStart = item.First.DepthStart,
DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours), DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours),
DepthEnd = item.Last.DepthEnd, DepthEnd = item.Last.DepthEnd,
}) });
.ToArray()
.AsEnumerable();
entry.Value = sections; entry.Value = sections;
return sections; return sections;
}); });
return cache!; result.AddRange(sections!);
}
return result;
} }
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken) public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)