Merge pull request 'Фикс ключа кэша' (#317) from fix/section_cache into dev

Reviewed-on: https://test.digitaldrilling.ru:8443/DDrilling/AsbCloudServer/pulls/317
This commit is contained in:
Никита Фролов 2024-09-04 15:19:12 +05:00
commit f9657d358b

View File

@ -20,6 +20,7 @@ namespace AsbCloudInfrastructure.Repository;
public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto, WellOperation>, public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto, WellOperation>,
IWellOperationRepository IWellOperationRepository
{ {
private const string keyCacheTemplate = "OperationsBySectionSummaries_{0}";
private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations"; private const string cacheKeyWellOperations = "FirstAndLastFactWellsOperations";
private readonly IMemoryCache memoryCache; private readonly IMemoryCache memoryCache;
private readonly IWellOperationCategoryRepository wellOperationCategoryRepository; private readonly IWellOperationCategoryRepository wellOperationCategoryRepository;
@ -109,13 +110,26 @@ 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)
{ {
const string keyCacheSections = "OperationsBySectionSummarties"; var result = new List<SectionByOperationsDto>();
var notFoundIds = new List<int>();
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<IEnumerable<SectionByOperationsDto>>(cacheKey, out var section))
{
result.AddRange(section!);
}
else
{
notFoundIds.Add(idWell);
}
}
if (notFoundIds.Count != 0)
{
var query = dbContext.Set<WellOperation>() var query = dbContext.Set<WellOperation>()
.Where(operation => notFoundIds.Contains( operation.IdWell))
.GroupBy(operation => new .GroupBy(operation => new
{ {
operation.IdWell, operation.IdWell,
@ -129,16 +143,10 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
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
@ -148,32 +156,36 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
operation.DepthEnd, operation.DepthEnd,
}) })
.First(), .First(),
})
.Where(s => idsWells.Contains(s.IdWell));
var dbData = await query.ToArrayAsync(token);
var sections = dbData.Select(
item => 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,
})
.ToArray()
.AsEnumerable();
entry.Value = sections;
return sections;
}); });
return cache!; var entities = await query.ToArrayAsync(token);
var dtos = entities.Select(
entity => new SectionByOperationsDto
{
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);
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<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken) public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)