diff --git a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs index 1eeb1e3a..dd85deef 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs @@ -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 cacheSectionsTypes; - private readonly CacheTable cacheWellType; - private readonly CacheTable 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((DbContext)db); - cacheWellType = cache.GetCachedTable((DbContext)db); - cacheCluster = cache.GetCachedTable((DbContext)db); } public async Task 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(db, token)) + .FirstOrDefault(c => c.Id == idCluster); var statClusterDto = new StatClusterDto { Id = idCluster, @@ -133,7 +131,9 @@ namespace AsbCloudInfrastructure.Services.WellOperationService private async Task CalcWellStatAsync(Well well, CancellationToken token = default) { - var wellType = await cacheWellType.FirstOrDefaultAsync(t => t.Id == well.IdWellType, token); + var wellType = (await memoryCache + .GetOrCreateBasicAsync(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(db) .Where(s => sectionTypeIds.Contains(s.Id)) .ToDictionary(s => s.Id);