forked from ddrilling/AsbCloudServer
CacheBase makes cache for TEntity only. Converter moved to CrudCacheRepositoryBase
This commit is contained in:
parent
a859c096d1
commit
289d1831b4
@ -1,7 +1,4 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Mapster;
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
@ -12,12 +9,12 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
public class CacheBase<TDto, TEntity> : QueryContainer<TEntity>
|
||||
where TDto : AsbCloudApp.Data.IId
|
||||
#nullable enable
|
||||
public class CacheBase<TEntity> : QueryContainer<TEntity>
|
||||
where TEntity : class, AsbCloudDb.Model.IId
|
||||
{
|
||||
protected readonly IMemoryCache memoryCache;
|
||||
protected string CacheTag = typeof(TDto).Name;
|
||||
protected string CacheTag = typeof(TEntity).Name;
|
||||
protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5);
|
||||
|
||||
public CacheBase(IAsbCloudDbContext context, IMemoryCache memoryCache)
|
||||
@ -35,7 +32,7 @@ namespace AsbCloudInfrastructure.Repository
|
||||
protected virtual void DropCache()
|
||||
=> memoryCache.Remove(CacheTag);
|
||||
|
||||
protected virtual IEnumerable<TDto> GetCache()
|
||||
protected virtual IEnumerable<TEntity> GetCache()
|
||||
{
|
||||
var cache = memoryCache.GetOrCreate(CacheTag, cacheEntry =>
|
||||
{
|
||||
@ -43,13 +40,12 @@ namespace AsbCloudInfrastructure.Repository
|
||||
cacheEntry.SlidingExpiration = CacheOlescence;
|
||||
|
||||
var entities = this.GetQuery().ToArray();
|
||||
var dtos = entities.Select(Convert);
|
||||
return dtos.ToArray();
|
||||
return entities;
|
||||
});
|
||||
return cache;
|
||||
}
|
||||
|
||||
protected virtual Task<IEnumerable<TDto>> GetCacheAsync(CancellationToken token)
|
||||
protected virtual Task<IEnumerable<TEntity>> GetCacheAsync(CancellationToken token)
|
||||
{
|
||||
var cache = memoryCache.GetOrCreateAsync(CacheTag, async (cacheEntry) =>
|
||||
{
|
||||
@ -57,14 +53,10 @@ namespace AsbCloudInfrastructure.Repository
|
||||
cacheEntry.SlidingExpiration = CacheOlescence;
|
||||
|
||||
var entities = await this.GetQuery().ToArrayAsync(token);
|
||||
var dtos = entities.Select(Convert);
|
||||
return dtos.ToArray().AsEnumerable();
|
||||
return entities.AsEnumerable();
|
||||
});
|
||||
return cache;
|
||||
}
|
||||
|
||||
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
||||
|
||||
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
|
||||
}
|
||||
#nullable disable
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
@ -16,7 +17,7 @@ namespace AsbCloudInfrastructure.Repository
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto"></typeparam>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
public class CrudCacheRepositoryBase<TDto, TEntity> : CacheBase<TDto, TEntity>, ICrudRepository<TDto>
|
||||
public class CrudCacheRepositoryBase<TDto, TEntity> : CacheBase<TEntity>, ICrudRepository<TDto>
|
||||
where TDto : AsbCloudApp.Data.IId
|
||||
where TEntity : class, IId
|
||||
{
|
||||
@ -36,7 +37,7 @@ namespace AsbCloudInfrastructure.Repository
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<int> InsertAsync(TDto newItem, CancellationToken token)
|
||||
public virtual async Task<int> InsertAsync(TDto newItem, CancellationToken token)
|
||||
{
|
||||
var result = await crudServiceBase.InsertAsync(newItem, token);
|
||||
if (result > 0)
|
||||
@ -45,7 +46,7 @@ namespace AsbCloudInfrastructure.Repository
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
||||
public virtual async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
||||
{
|
||||
var result = await crudServiceBase.InsertRangeAsync(dtos, token);
|
||||
if (result > 0)
|
||||
@ -53,11 +54,21 @@ namespace AsbCloudInfrastructure.Repository
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual async Task<int> UpdateAsync(TDto dto, CancellationToken token)
|
||||
{
|
||||
var result = await crudServiceBase.UpdateAsync(dto, token);
|
||||
if (result > 0)
|
||||
DropCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
|
||||
{
|
||||
var cache = await GetCacheAsync(token);
|
||||
return cache;
|
||||
var dtos = cache.Select(Convert);
|
||||
return dtos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -68,33 +79,36 @@ namespace AsbCloudInfrastructure.Repository
|
||||
public TDto? GetOrDefault(int id)
|
||||
{
|
||||
var cache = GetCache();
|
||||
return cache.FirstOrDefault(d => d.Id == id);
|
||||
var cacheItem = cache.FirstOrDefault(d => d.Id == id);
|
||||
if (cacheItem is null)
|
||||
return default;
|
||||
var dto = Convert(cacheItem);
|
||||
return dto;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
||||
{
|
||||
var cache = await GetCacheAsync(token);
|
||||
return cache.FirstOrDefault(d => d.Id == id);
|
||||
var cacheItem = cache.FirstOrDefault(d => d.Id == id);
|
||||
if (cacheItem is null)
|
||||
return default;
|
||||
var dto = Convert(cacheItem);
|
||||
return dto;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<int> UpdateAsync(TDto dto, CancellationToken token)
|
||||
{
|
||||
var result = await crudServiceBase.UpdateAsync(dto, token);
|
||||
if (result > 0)
|
||||
DropCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<int> DeleteAsync(int id, CancellationToken token)
|
||||
public virtual async Task<int> DeleteAsync(int id, CancellationToken token)
|
||||
{
|
||||
var result = await crudServiceBase.DeleteAsync(id, token);
|
||||
if (result > 0)
|
||||
DropCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
|
||||
|
||||
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
|
||||
}
|
||||
#nullable disable
|
||||
}
|
@ -27,7 +27,7 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
var dtos = cache
|
||||
.Where(e => e.IdWell == idWell)
|
||||
.ToList();
|
||||
.Select(Convert);
|
||||
|
||||
return dtos;
|
||||
}
|
||||
@ -41,7 +41,8 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
var dtos = cache
|
||||
.Where(e => idsWells.Contains(e.IdWell))
|
||||
.ToList();
|
||||
.Select(Convert);
|
||||
|
||||
return dtos;
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,8 @@ namespace AsbCloudInfrastructure.Services
|
||||
.ConfigureAwait(false);
|
||||
var dtos = cache
|
||||
.Where(f => f.Id >= 10000)
|
||||
.Where(f => f.Id <= 20000);
|
||||
.Where(f => f.Id <= 20000)
|
||||
.Select(Convert);
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
@ -81,12 +81,13 @@ namespace AsbCloudInfrastructure.Services
|
||||
.Select(r => r.IdWell);
|
||||
|
||||
var wellsDtos = (await GetCacheAsync(token))
|
||||
.Where(w => wellsIds.Contains(w.Id));
|
||||
.Where(w => wellsIds.Contains(w.Id))
|
||||
.Select(Convert);
|
||||
|
||||
return wellsDtos.ToList();
|
||||
return wellsDtos;
|
||||
}
|
||||
|
||||
public async Task<int> InsertAsync(WellDto dto, CancellationToken token = default)
|
||||
public override async Task<int> InsertAsync(WellDto dto, CancellationToken token = default)
|
||||
{
|
||||
if (dto.IdWellType is < 1 or > 2)
|
||||
throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto));
|
||||
@ -112,12 +113,12 @@ namespace AsbCloudInfrastructure.Services
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<int> InsertRangeAsync(IEnumerable<WellDto> dtos, CancellationToken token)
|
||||
public override Task<int> InsertRangeAsync(IEnumerable<WellDto> dtos, CancellationToken token)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<int> UpdateAsync(WellDto dto,
|
||||
public override async Task<int> UpdateAsync(WellDto dto,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
if (dto.IdWellType is < 1 or > 2)
|
||||
@ -154,9 +155,8 @@ namespace AsbCloudInfrastructure.Services
|
||||
|
||||
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
|
||||
var dto = Convert(entity);
|
||||
return dto.Caption;
|
||||
var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
|
||||
return entity!.Caption;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token)
|
||||
|
Loading…
Reference in New Issue
Block a user