replace CacheDb in WellOperationService by ms.MemoryCache

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

View File

@ -1,9 +1,9 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -16,9 +16,8 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
public class WellOperationService : IWellOperationService public class WellOperationService : IWellOperationService
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly IWellService wellService; private readonly IWellService wellService;
private readonly CacheTable<WellOperationCategory> cachedOperationCategories;
private readonly CacheTable<WellSectionType> cachedSectionTypes;
private Dictionary<int, DateTimeOffset?>? firstOperationsCache = null; private Dictionary<int, DateTimeOffset?>? firstOperationsCache = null;
@ -33,22 +32,25 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
public const int idOperationTypeFact = 1; public const int idOperationTypeFact = 1;
public const int idOperationTypeRepair = 1031; 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.db = db;
this.memoryCache = memoryCache;
this.wellService = wellService; this.wellService = wellService;
cachedOperationCategories = cache.GetCachedTable<WellOperationCategory>((DbContext)db);
cachedSectionTypes = cache.GetCachedTable<WellSectionType>((DbContext)db);
} }
public IDictionary<int, string> GetSectionTypes() 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() public IEnumerable<WellOperationCategoryDto> GetCategories()
{ {
var operationTypes = cachedOperationCategories var operationCategories = memoryCache
.Distinct().OrderBy(o => o.Name); .GetOrCreateBasic<WellOperationCategory>(db)
var result = operationTypes.Adapt<IEnumerable<WellOperationCategoryDto>>(); .Distinct()
.OrderBy(o => o.Name);
var result = operationCategories.Adapt<IEnumerable<WellOperationCategoryDto>>();
return result; return result;
} }

View File

@ -43,7 +43,7 @@ namespace AsbCloudInfrastructure.Services
this.telemetryService = telemetryService; this.telemetryService = telemetryService;
this.timezoneService = timezoneService; 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); companyTypesService = new CrudCacheServiceBase<CompanyTypeDto, CompanyType>(dbContext, memoryCache);
} }