replace CacheDb in OperationsStatService by IMemoryCache

This commit is contained in:
ngfrolov 2022-11-18 15:25:38 +05:00
parent e8ae5674c1
commit e837baf5e7

View File

@ -1,9 +1,9 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
@ -15,18 +15,14 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
public class OperationsStatService : IOperationsStatService
{
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly IWellService wellService;
private readonly CacheTable<WellSectionType> cacheSectionsTypes;
private readonly CacheTable<WellType> cacheWellType;
private readonly CacheTable<Cluster> cacheCluster;
public OperationsStatService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService)
public OperationsStatService(IAsbCloudDbContext db, IMemoryCache memoryCache, IWellService wellService)
{
this.db = db;
this.memoryCache = memoryCache;
this.wellService = wellService;
cacheSectionsTypes = cache.GetCachedTable<WellSectionType>((DbContext)db);
cacheWellType = cache.GetCachedTable<WellType>((DbContext)db);
cacheCluster = cache.GetCachedTable<Cluster>((DbContext)db);
}
public async Task<StatClusterDto> GetStatClusterAsync(int idCluster, int idCompany, CancellationToken token = default)
@ -44,7 +40,9 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
var statsWells = await GetWellsStatAsync(wells, token).ConfigureAwait(false);
var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token);
var cluster = (await memoryCache
.GetOrCreateBasicAsync<Cluster>(db, token))
.FirstOrDefault(c => c.Id == idCluster);
var statClusterDto = new StatClusterDto
{
Id = idCluster,
@ -133,7 +131,9 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
private async Task<StatWellDto> CalcWellStatAsync(Well well, CancellationToken token = default)
{
var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token);
var wellType = (await memoryCache
.GetOrCreateBasicAsync<WellType>(db, token))
.FirstOrDefault(t => t.Id == well.IdWellType);
var statWellDto = new StatWellDto()
{
Id = well.Id,
@ -169,7 +169,8 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
.Select(o => o.IdWellSectionType)
.Distinct();
var sectionTypes = cacheSectionsTypes
var sectionTypes = memoryCache
.GetOrCreateBasic<WellSectionType>(db)
.Where(s => sectionTypeIds.Contains(s.Id))
.ToDictionary(s => s.Id);