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>,
|
||||
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<WellOperationBaseDto,
|
||||
|
||||
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>()
|
||||
.Where(operation => notFoundIds.Contains( operation.IdWell))
|
||||
.GroupBy(operation => new
|
||||
{
|
||||
operation.IdWell,
|
||||
@ -129,16 +143,10 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
|
||||
group.Key.IdType,
|
||||
group.Key.IdWellSectionType,
|
||||
group.Key.Caption,
|
||||
|
||||
First = group
|
||||
.OrderBy(operation => operation.DateStart)
|
||||
.Select(operation => new
|
||||
{
|
||||
operation.DateStart,
|
||||
operation.DepthStart,
|
||||
})
|
||||
.Select(operation => new { operation.DateStart, operation.DepthStart, })
|
||||
.First(),
|
||||
|
||||
Last = group
|
||||
.OrderByDescending(operation => operation.DateStart)
|
||||
.Select(operation => new
|
||||
@ -148,32 +156,36 @@ public class WellOperationRepository : CrudRepositoryBase<WellOperationBaseDto,
|
||||
operation.DepthEnd,
|
||||
})
|
||||
.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)
|
||||
|
Loading…
Reference in New Issue
Block a user