Trying to use microsoft in memeory cache

This commit is contained in:
ngfrolov 2022-11-17 16:09:51 +05:00
parent ec3a0e00ef
commit fa0389c08f
10 changed files with 62 additions and 58 deletions

View File

@ -16,10 +16,10 @@ using AsbCloudInfrastructure.Services.SAUB;
using AsbCloudInfrastructure.Services.Subsystems; using AsbCloudInfrastructure.Services.Subsystems;
using AsbCloudInfrastructure.Services.WellOperationService; using AsbCloudInfrastructure.Services.WellOperationService;
using AsbCloudInfrastructure.Validators; using AsbCloudInfrastructure.Validators;
using DocumentFormat.OpenXml.Spreadsheet;
using FluentValidation.AspNetCore; using FluentValidation.AspNetCore;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
@ -93,6 +93,7 @@ namespace AsbCloudInfrastructure
services.AddFluentValidation(); services.AddFluentValidation();
services.AddMemoryCache();
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>()); services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>());
services.AddScoped<IEmailService, EmailService>(); services.AddScoped<IEmailService, EmailService>();
@ -140,21 +141,25 @@ namespace AsbCloudInfrastructure
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>(s => services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>(s =>
new CrudCacheServiceBase<TelemetryDto, Telemetry>( new CrudCacheServiceBase<TelemetryDto, Telemetry>(
s.GetService<IAsbCloudDbContext>(), s.GetService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(),
dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>(); services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
services.AddTransient<ICrudService<DepositDto>, CrudCacheServiceBase<DepositDto, Deposit>>(s => services.AddTransient<ICrudService<DepositDto>, CrudCacheServiceBase<DepositDto, Deposit>>(s =>
new CrudCacheServiceBase<DepositDto, Deposit>( new CrudCacheServiceBase<DepositDto, Deposit>(
s.GetService<IAsbCloudDbContext>(), s.GetService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(),
dbSet => dbSet.Include(d => d.Clusters))); dbSet => dbSet.Include(d => d.Clusters)));
services.AddTransient<ICrudService<CompanyDto>, CrudCacheServiceBase<CompanyDto, Company>>(s => services.AddTransient<ICrudService<CompanyDto>, CrudCacheServiceBase<CompanyDto, Company>>(s =>
new CrudCacheServiceBase<CompanyDto, Company>( new CrudCacheServiceBase<CompanyDto, Company>(
s.GetService<IAsbCloudDbContext>(), s.GetService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(),
dbSet => dbSet.Include(c => c.CompanyType))); dbSet => dbSet.Include(c => c.CompanyType)));
services.AddTransient<ICrudService<CompanyTypeDto>, CrudCacheServiceBase<CompanyTypeDto, CompanyType>>(); services.AddTransient<ICrudService<CompanyTypeDto>, CrudCacheServiceBase<CompanyTypeDto, CompanyType>>();
services.AddTransient<ICrudService<ClusterDto>, CrudCacheServiceBase<ClusterDto, Cluster>>(s => services.AddTransient<ICrudService<ClusterDto>, CrudCacheServiceBase<ClusterDto, Cluster>>(s =>
new CrudCacheServiceBase<ClusterDto, Cluster>( new CrudCacheServiceBase<ClusterDto, Cluster>(
s.GetService<IAsbCloudDbContext>(), s.GetService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(),
dbSet => dbSet dbSet => dbSet
.Include(c => c.Wells) .Include(c => c.Wells)
.Include(c => c.Deposit))); // может быть включен в сервис ClusterService .Include(c => c.Deposit))); // может быть включен в сервис ClusterService

View File

@ -1,6 +1,6 @@
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.EfCache;
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;
@ -21,16 +21,21 @@ namespace AsbCloudInfrastructure.Repository
{ {
protected string CacheTag = typeof(TDto).Name; protected string CacheTag = typeof(TDto).Name;
protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5); protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5);
private readonly IMemoryCache memoryCache;
protected int KeySelector(TEntity entity) => entity.Id; protected int KeySelector(TEntity entity) => entity.Id;
public CrudCacheServiceBase(IAsbCloudDbContext dbContext) public CrudCacheServiceBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
: base(dbContext) { } : base(dbContext)
{
this.memoryCache = memoryCache;
}
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes) public CrudCacheServiceBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
: base(dbContext, includes) { } : base(dbContext, makeQuery)
{
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery) this.memoryCache = memoryCache;
: base(dbContext, makeQuery) { } }
/// <inheritdoc/> /// <inheritdoc/>
public override async Task<int> InsertAsync(TDto newItem, CancellationToken token) public override async Task<int> InsertAsync(TDto newItem, CancellationToken token)
@ -103,16 +108,33 @@ namespace AsbCloudInfrastructure.Repository
} }
protected virtual Task<IEnumerable<TDto>> GetCacheAsync(CancellationToken token) protected virtual Task<IEnumerable<TDto>> GetCacheAsync(CancellationToken token)
=> GetQuery() {
.FromCacheAsync(CacheTag, CacheOlescence, Convert, token); var cache = memoryCache.GetOrCreateAsync(CacheTag, async (cacheEntry) => {
cacheEntry.AbsoluteExpirationRelativeToNow = CacheOlescence;
cacheEntry.SlidingExpiration = CacheOlescence;
var entities = await GetQuery().ToArrayAsync(token);
var dtos = entities.Select(Convert);
return dtos.ToArray().AsEnumerable();
});
return cache;
}
protected virtual IEnumerable<TDto> GetCache() protected virtual IEnumerable<TDto> GetCache()
=> GetQuery() {
.FromCache(CacheTag, CacheOlescence, Convert); var cache = memoryCache.GetOrCreate(CacheTag, cacheEntry => {
cacheEntry.AbsoluteExpirationRelativeToNow = CacheOlescence;
cacheEntry.SlidingExpiration= CacheOlescence;
var entities = GetQuery().ToArray();
var dtos = entities.Select(Convert);
return dtos.ToArray();
});
return cache;
}
protected virtual void DropCache() protected virtual void DropCache()
=> dbSet.DropCache(CacheTag); => memoryCache.Remove(CacheTag);
} }
#nullable disable #nullable disable
} }

View File

@ -31,20 +31,6 @@ namespace AsbCloudInfrastructure.Repository
GetQuery = () => dbSet; GetQuery = () => dbSet;
} }
public CrudServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
{
this.dbContext = dbContext;
dbSet = dbContext.Set<TEntity>();
GetQuery = () =>
{
IQueryable<TEntity> query = dbSet;
foreach (var include in includes)
query = query.Include(include);
return query;
};
}
public CrudServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery) public CrudServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
{ {
dbContext = context; dbContext = context;

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
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;
@ -14,14 +15,11 @@ namespace AsbCloudInfrastructure.Repository
where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated
where TEntity : class, IId, IWellRelated where TEntity : class, IId, IWellRelated
{ {
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context) public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, IMemoryCache memoryCache)
: base(context) { } : base(context, memoryCache) { }
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes) public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
: base(dbContext, includes) { } : base(context, memoryCache, makeQuery) { }
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
: base(context, makeQuery) { }
public async Task<IEnumerable<TDto>?> GetByIdWellAsync(int idWell, CancellationToken token) public async Task<IEnumerable<TDto>?> GetByIdWellAsync(int idWell, CancellationToken token)
{ {

View File

@ -17,9 +17,6 @@ namespace AsbCloudInfrastructure.Repository
public CrudWellRelatedServiceBase(IAsbCloudDbContext context) public CrudWellRelatedServiceBase(IAsbCloudDbContext context)
: base(context) { } : base(context) { }
public CrudWellRelatedServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
: base(dbContext, includes) { }
public CrudWellRelatedServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery) public CrudWellRelatedServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
: base(context, makeQuery) { } : base(context, makeQuery) { }

View File

@ -2,6 +2,7 @@
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System; using System;
namespace AsbCloudInfrastructure.Repository namespace AsbCloudInfrastructure.Repository
@ -10,8 +11,8 @@ namespace AsbCloudInfrastructure.Repository
{ {
private readonly IWellService wellService; private readonly IWellService wellService;
public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IWellService wellService) public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, IWellService wellService)
: base(dbContext, q => q.Include(s => s.Author) : base(dbContext, memoryCache, q => q.Include(s => s.Author)
.Include(s => s.Well)) .Include(s => s.Well))
{ {
this.wellService = wellService; this.wellService = wellService;

View File

@ -2,6 +2,7 @@
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Repository;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -11,10 +12,8 @@ namespace AsbCloudInfrastructure.Services
{ {
public class FileCategoryService : CrudCacheServiceBase<FileCategoryDto, FileCategory>, IFileCategoryService public class FileCategoryService : CrudCacheServiceBase<FileCategoryDto, FileCategory>, IFileCategoryService
{ {
public FileCategoryService(IAsbCloudDbContext context) public FileCategoryService(IAsbCloudDbContext context, IMemoryCache memoryCache)
: base(context) : base(context, memoryCache) { }
{
}
//TODO: Перенести в сервис "дело скважины" //TODO: Перенести в сервис "дело скважины"
public async Task<IEnumerable<FileCategoryDto>> GetWellCaseCategoriesAsync(CancellationToken token) public async Task<IEnumerable<FileCategoryDto>> GetWellCaseCategoriesAsync(CancellationToken token)

View File

@ -3,6 +3,7 @@ using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Repository;
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;
@ -30,9 +31,9 @@ namespace AsbCloudInfrastructure.Services.SAUB
private readonly SetpointsRequestRepository setpointsRepository; private readonly SetpointsRequestRepository setpointsRepository;
private readonly ITelemetryService telemetryService; private readonly ITelemetryService telemetryService;
public SetpointsService(IAsbCloudDbContext db, ITelemetryService telemetryService, IWellService wellService) public SetpointsService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService, IWellService wellService)
{ {
setpointsRepository = new SetpointsRequestRepository(db, wellService); setpointsRepository = new SetpointsRequestRepository(db, memoryCache, wellService);
this.telemetryService = telemetryService; this.telemetryService = telemetryService;
} }

View File

@ -6,6 +6,7 @@ using AsbCloudDb.Model.Subsystems;
using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Repository;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -17,7 +18,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
internal class SubsystemService : CrudCacheServiceBase<SubsystemDto, Subsystem>, ISubsystemService internal class SubsystemService : CrudCacheServiceBase<SubsystemDto, Subsystem>, ISubsystemService
{ {
private readonly IWellService wellService; private readonly IWellService wellService;
public SubsystemService(IAsbCloudDbContext dbContext, IWellService wellService) : base(dbContext) public SubsystemService(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, IWellService wellService) : base(dbContext, memoryCache)
{ {
this.wellService = wellService; this.wellService = wellService;
} }

View File

@ -7,6 +7,7 @@ using AsbCloudInfrastructure.Repository;
using AsbCloudInfrastructure.Services.Cache; 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;
@ -34,16 +35,16 @@ namespace AsbCloudInfrastructure.Services
.Include(w => w.Telemetry) .Include(w => w.Telemetry)
.Include(w => w.WellType) .Include(w => w.WellType)
.Include(w => w.RelationCompaniesWells) .Include(w => w.RelationCompaniesWells)
.ThenInclude(r => r.Company); .ThenInclude(r => r.Company);
public WellService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService, ITimezoneService timezoneService) public WellService(IAsbCloudDbContext db, IMemoryCache memoryCache, CacheDb cacheDb, ITelemetryService telemetryService, ITimezoneService timezoneService)
: base(db, MakeQueryWell) : base(db, memoryCache, MakeQueryWell)
{ {
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, cacheDb, this);
companyTypesService = new CrudCacheServiceBase<CompanyTypeDto, CompanyType>(dbContext); companyTypesService = new CrudCacheServiceBase<CompanyTypeDto, CompanyType>(dbContext, memoryCache);
} }
private IEnumerable<RelationCompanyWell> GetCacheRelationCompanyWell() private IEnumerable<RelationCompanyWell> GetCacheRelationCompanyWell()
@ -167,13 +168,6 @@ namespace AsbCloudInfrastructure.Services
return dtos; return dtos;
} }
private IEnumerable<CompanyDto> GetCompanies(int idWell)
{
var relations = GetCacheRelationCompanyWell().Where(r => r.IdWell == idWell);
var dtos = relations.Select(r => Convert(r.Company));
return dtos;
}
public string GetStateText(int state) public string GetStateText(int state)
{ {
return state switch return state switch