forked from ddrilling/AsbCloudServer
Trying to use microsoft in memeory cache
This commit is contained in:
parent
ec3a0e00ef
commit
fa0389c08f
@ -16,10 +16,10 @@ using AsbCloudInfrastructure.Services.SAUB;
|
||||
using AsbCloudInfrastructure.Services.Subsystems;
|
||||
using AsbCloudInfrastructure.Services.WellOperationService;
|
||||
using AsbCloudInfrastructure.Validators;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
@ -93,6 +93,7 @@ namespace AsbCloudInfrastructure
|
||||
|
||||
services.AddFluentValidation();
|
||||
|
||||
services.AddMemoryCache();
|
||||
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>());
|
||||
services.AddScoped<IEmailService, EmailService>();
|
||||
|
||||
@ -140,21 +141,25 @@ namespace AsbCloudInfrastructure
|
||||
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>(s =>
|
||||
new CrudCacheServiceBase<TelemetryDto, Telemetry>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService
|
||||
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
|
||||
services.AddTransient<ICrudService<DepositDto>, CrudCacheServiceBase<DepositDto, Deposit>>(s =>
|
||||
new CrudCacheServiceBase<DepositDto, Deposit>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
dbSet => dbSet.Include(d => d.Clusters)));
|
||||
services.AddTransient<ICrudService<CompanyDto>, CrudCacheServiceBase<CompanyDto, Company>>(s =>
|
||||
new CrudCacheServiceBase<CompanyDto, Company>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
dbSet => dbSet.Include(c => c.CompanyType)));
|
||||
|
||||
services.AddTransient<ICrudService<CompanyTypeDto>, CrudCacheServiceBase<CompanyTypeDto, CompanyType>>();
|
||||
services.AddTransient<ICrudService<ClusterDto>, CrudCacheServiceBase<ClusterDto, Cluster>>(s =>
|
||||
new CrudCacheServiceBase<ClusterDto, Cluster>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
dbSet => dbSet
|
||||
.Include(c => c.Wells)
|
||||
.Include(c => c.Deposit))); // может быть включен в сервис ClusterService
|
||||
|
@ -1,6 +1,6 @@
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.EfCache;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -21,16 +21,21 @@ namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
protected string CacheTag = typeof(TDto).Name;
|
||||
protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5);
|
||||
private readonly IMemoryCache memoryCache;
|
||||
|
||||
protected int KeySelector(TEntity entity) => entity.Id;
|
||||
|
||||
public CrudCacheServiceBase(IAsbCloudDbContext dbContext)
|
||||
: base(dbContext) { }
|
||||
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
|
||||
: base(dbContext)
|
||||
{
|
||||
this.memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
|
||||
: base(dbContext, includes) { }
|
||||
|
||||
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
||||
: base(dbContext, makeQuery) { }
|
||||
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
||||
: base(dbContext, makeQuery)
|
||||
{
|
||||
this.memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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)
|
||||
=> 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()
|
||||
=> 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()
|
||||
=> dbSet.DropCache(CacheTag);
|
||||
=> memoryCache.Remove(CacheTag);
|
||||
}
|
||||
#nullable disable
|
||||
}
|
@ -31,20 +31,6 @@ namespace AsbCloudInfrastructure.Repository
|
||||
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)
|
||||
{
|
||||
dbContext = context;
|
||||
|
@ -1,6 +1,7 @@
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -14,14 +15,11 @@ namespace AsbCloudInfrastructure.Repository
|
||||
where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated
|
||||
where TEntity : class, IId, IWellRelated
|
||||
{
|
||||
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context)
|
||||
: base(context) { }
|
||||
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, IMemoryCache memoryCache)
|
||||
: base(context, memoryCache) { }
|
||||
|
||||
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
|
||||
: base(dbContext, includes) { }
|
||||
|
||||
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
||||
: base(context, makeQuery) { }
|
||||
public CrudWellRelatedCacheServiceBase(IAsbCloudDbContext context, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
||||
: base(context, memoryCache, makeQuery) { }
|
||||
|
||||
public async Task<IEnumerable<TDto>?> GetByIdWellAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
|
@ -17,9 +17,6 @@ namespace AsbCloudInfrastructure.Repository
|
||||
public CrudWellRelatedServiceBase(IAsbCloudDbContext context)
|
||||
: base(context) { }
|
||||
|
||||
public CrudWellRelatedServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
|
||||
: base(dbContext, includes) { }
|
||||
|
||||
public CrudWellRelatedServiceBase(IAsbCloudDbContext context, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
||||
: base(context, makeQuery) { }
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
|
||||
namespace AsbCloudInfrastructure.Repository
|
||||
@ -10,8 +11,8 @@ namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
private readonly IWellService wellService;
|
||||
|
||||
public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IWellService wellService)
|
||||
: base(dbContext, q => q.Include(s => s.Author)
|
||||
public SetpointsRequestRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, IWellService wellService)
|
||||
: base(dbContext, memoryCache, q => q.Include(s => s.Author)
|
||||
.Include(s => s.Well))
|
||||
{
|
||||
this.wellService = wellService;
|
||||
|
@ -2,6 +2,7 @@
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.Repository;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
@ -11,10 +12,8 @@ namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
public class FileCategoryService : CrudCacheServiceBase<FileCategoryDto, FileCategory>, IFileCategoryService
|
||||
{
|
||||
public FileCategoryService(IAsbCloudDbContext context)
|
||||
: base(context)
|
||||
{
|
||||
}
|
||||
public FileCategoryService(IAsbCloudDbContext context, IMemoryCache memoryCache)
|
||||
: base(context, memoryCache) { }
|
||||
|
||||
//TODO: Перенести в сервис "дело скважины"
|
||||
public async Task<IEnumerable<FileCategoryDto>> GetWellCaseCategoriesAsync(CancellationToken token)
|
||||
|
@ -3,6 +3,7 @@ using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.Repository;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -30,9 +31,9 @@ namespace AsbCloudInfrastructure.Services.SAUB
|
||||
private readonly SetpointsRequestRepository setpointsRepository;
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ using AsbCloudDb.Model.Subsystems;
|
||||
using AsbCloudInfrastructure.Repository;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
@ -17,7 +18,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
||||
internal class SubsystemService : CrudCacheServiceBase<SubsystemDto, Subsystem>, ISubsystemService
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ using AsbCloudInfrastructure.Repository;
|
||||
using AsbCloudInfrastructure.Services.Cache;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -34,16 +35,16 @@ namespace AsbCloudInfrastructure.Services
|
||||
.Include(w => w.Telemetry)
|
||||
.Include(w => w.WellType)
|
||||
.Include(w => w.RelationCompaniesWells)
|
||||
.ThenInclude(r => r.Company);
|
||||
.ThenInclude(r => r.Company);
|
||||
|
||||
public WellService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService, ITimezoneService timezoneService)
|
||||
: base(db, MakeQueryWell)
|
||||
public WellService(IAsbCloudDbContext db, IMemoryCache memoryCache, CacheDb cacheDb, ITelemetryService telemetryService, ITimezoneService timezoneService)
|
||||
: base(db, memoryCache, MakeQueryWell)
|
||||
{
|
||||
this.telemetryService = telemetryService;
|
||||
this.timezoneService = timezoneService;
|
||||
|
||||
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()
|
||||
@ -167,13 +168,6 @@ namespace AsbCloudInfrastructure.Services
|
||||
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)
|
||||
{
|
||||
return state switch
|
||||
|
Loading…
Reference in New Issue
Block a user