replace CacheDb in WellOperationService by ms.MemoryCache

This commit is contained in:
ngfrolov 2022-11-18 15:17:04 +05:00
parent 04414b3c75
commit ede40c9a23
2 changed files with 14 additions and 12 deletions

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;
@ -16,9 +16,8 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
public class WellOperationService : IWellOperationService
{
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly IWellService wellService;
private readonly CacheTable<WellOperationCategory> cachedOperationCategories;
private readonly CacheTable<WellSectionType> cachedSectionTypes;
private Dictionary<int, DateTimeOffset?>? firstOperationsCache = null;
@ -33,22 +32,25 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
public const int idOperationTypeFact = 1;
public const int idOperationTypeRepair = 1031;
public WellOperationService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService)
public WellOperationService(IAsbCloudDbContext db, IMemoryCache memoryCache, IWellService wellService)
{
this.db = db;
this.memoryCache = memoryCache;
this.wellService = wellService;
cachedOperationCategories = cache.GetCachedTable<WellOperationCategory>((DbContext)db);
cachedSectionTypes = cache.GetCachedTable<WellSectionType>((DbContext)db);
}
public IDictionary<int, string> GetSectionTypes()
=> cachedSectionTypes.ToDictionary(s => s.Id, s => s.Caption);
=> memoryCache
.GetOrCreateBasic<WellSectionType>(db)
.ToDictionary(s => s.Id, s => s.Caption);
public IEnumerable<WellOperationCategoryDto> GetCategories()
{
var operationTypes = cachedOperationCategories
.Distinct().OrderBy(o => o.Name);
var result = operationTypes.Adapt<IEnumerable<WellOperationCategoryDto>>();
var operationCategories = memoryCache
.GetOrCreateBasic<WellOperationCategory>(db)
.Distinct()
.OrderBy(o => o.Name);
var result = operationCategories.Adapt<IEnumerable<WellOperationCategoryDto>>();
return result;
}

View File

@ -37,13 +37,13 @@ namespace AsbCloudInfrastructure.Services
.Include(w => w.RelationCompaniesWells)
.ThenInclude(r => r.Company);
public WellService(IAsbCloudDbContext db, IMemoryCache memoryCache, CacheDb cacheDb, ITelemetryService telemetryService, ITimezoneService timezoneService)
public WellService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService, ITimezoneService timezoneService)
: base(db, memoryCache, MakeQueryWell)
{
this.telemetryService = telemetryService;
this.timezoneService = timezoneService;
this.wellOperationService = new WellOperationService.WellOperationService(db, cacheDb, this);
this.wellOperationService = new WellOperationService.WellOperationService(db, memoryCache, this);
companyTypesService = new CrudCacheServiceBase<CompanyTypeDto, CompanyType>(dbContext, memoryCache);
}