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

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)
{
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)
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
var sections = await memoryCache.GetOrCreateAsync(string.Format(keyCacheTemplate, idWell), async (entry) =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
var query = dbContext.Set<WellOperation>()
.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,
var query = dbContext.Set<WellOperation>()
.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
.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(),
});
First = group
.OrderBy(operation => operation.DateStart)
.Select(operation => new
{
operation.DateStart,
operation.DepthStart,
})
.First(),
var dbData = await query.ToArrayAsync(token);
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(
var sections = dbData.Select(
item => new SectionByOperationsDto
{
IdWell = item.IdWell,
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,
})
.ToArray()
.AsEnumerable();
});
entry.Value = sections;
return sections;
});
return cache!;
entry.Value = sections;
return sections;
});
result.AddRange(sections!);
}
return result;
}
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)