forked from ddrilling/AsbCloudServer
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:
commit
f9657d358b
@ -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,51 +143,49 @@ 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, })
|
||||||
{
|
.First(),
|
||||||
operation.DateStart,
|
|
||||||
operation.DepthStart,
|
|
||||||
})
|
|
||||||
.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.DurationHours,
|
||||||
operation.DepthEnd,
|
operation.DepthEnd,
|
||||||
})
|
})
|
||||||
.First(),
|
.First(),
|
||||||
})
|
});
|
||||||
.Where(s => idsWells.Contains(s.IdWell));
|
|
||||||
var dbData = await query.ToArrayAsync(token);
|
var entities = await query.ToArrayAsync(token);
|
||||||
var sections = dbData.Select(
|
var dtos = entities.Select(
|
||||||
item => new SectionByOperationsDto
|
entity => new SectionByOperationsDto
|
||||||
{
|
{
|
||||||
IdWell = item.IdWell,
|
IdWell = entity.IdWell,
|
||||||
IdType = item.IdType,
|
IdType = entity.IdType,
|
||||||
IdWellSectionType = item.IdWellSectionType,
|
IdWellSectionType = entity.IdWellSectionType,
|
||||||
|
Caption = entity.Caption,
|
||||||
Caption = item.Caption,
|
DateStart = entity.First.DateStart,
|
||||||
|
DepthStart = entity.First.DepthStart,
|
||||||
DateStart = item.First.DateStart,
|
DateEnd = entity.Last.DateStart.AddHours(entity.Last.DurationHours),
|
||||||
DepthStart = item.First.DepthStart,
|
DepthEnd = entity.Last.DepthEnd,
|
||||||
|
|
||||||
DateEnd = item.Last.DateStart.AddHours(item.Last.DurationHours),
|
|
||||||
DepthEnd = item.Last.DepthEnd,
|
|
||||||
})
|
})
|
||||||
.ToArray()
|
.ToList();
|
||||||
.AsEnumerable();
|
|
||||||
|
|
||||||
entry.Value = sections;
|
result.AddRange(dtos);
|
||||||
return sections;
|
|
||||||
});
|
|
||||||
|
|
||||||
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<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
public async Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken)
|
||||||
|
Loading…
Reference in New Issue
Block a user