CacheBase makes cache for TEntity only. Converter moved to CrudCacheRepositoryBase

This commit is contained in:
ngfrolov 2022-12-05 09:36:02 +05:00
parent a859c096d1
commit 289d1831b4
5 changed files with 52 additions and 44 deletions

View File

@ -1,7 +1,4 @@
using AsbCloudApp.Data; using AsbCloudDb.Model;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System; using System;
@ -12,12 +9,12 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository namespace AsbCloudInfrastructure.Repository
{ {
public class CacheBase<TDto, TEntity> : QueryContainer<TEntity> #nullable enable
where TDto : AsbCloudApp.Data.IId public class CacheBase<TEntity> : QueryContainer<TEntity>
where TEntity : class, AsbCloudDb.Model.IId where TEntity : class, AsbCloudDb.Model.IId
{ {
protected readonly IMemoryCache memoryCache; protected readonly IMemoryCache memoryCache;
protected string CacheTag = typeof(TDto).Name; protected string CacheTag = typeof(TEntity).Name;
protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5); protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5);
public CacheBase(IAsbCloudDbContext context, IMemoryCache memoryCache) public CacheBase(IAsbCloudDbContext context, IMemoryCache memoryCache)
@ -35,7 +32,7 @@ namespace AsbCloudInfrastructure.Repository
protected virtual void DropCache() protected virtual void DropCache()
=> memoryCache.Remove(CacheTag); => memoryCache.Remove(CacheTag);
protected virtual IEnumerable<TDto> GetCache() protected virtual IEnumerable<TEntity> GetCache()
{ {
var cache = memoryCache.GetOrCreate(CacheTag, cacheEntry => var cache = memoryCache.GetOrCreate(CacheTag, cacheEntry =>
{ {
@ -43,13 +40,12 @@ namespace AsbCloudInfrastructure.Repository
cacheEntry.SlidingExpiration = CacheOlescence; cacheEntry.SlidingExpiration = CacheOlescence;
var entities = this.GetQuery().ToArray(); var entities = this.GetQuery().ToArray();
var dtos = entities.Select(Convert); return entities;
return dtos.ToArray();
}); });
return cache; 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) => var cache = memoryCache.GetOrCreateAsync(CacheTag, async (cacheEntry) =>
{ {
@ -57,14 +53,10 @@ namespace AsbCloudInfrastructure.Repository
cacheEntry.SlidingExpiration = CacheOlescence; cacheEntry.SlidingExpiration = CacheOlescence;
var entities = await this.GetQuery().ToArrayAsync(token); var entities = await this.GetQuery().ToArrayAsync(token);
var dtos = entities.Select(Convert); return entities.AsEnumerable();
return dtos.ToArray().AsEnumerable();
}); });
return cache; return cache;
} }
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
} }
#nullable disable
} }

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System; using System;
@ -16,7 +17,7 @@ namespace AsbCloudInfrastructure.Repository
/// </summary> /// </summary>
/// <typeparam name="TDto"></typeparam> /// <typeparam name="TDto"></typeparam>
/// <typeparam name="TEntity"></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 TDto : AsbCloudApp.Data.IId
where TEntity : class, IId where TEntity : class, IId
{ {
@ -36,7 +37,7 @@ namespace AsbCloudInfrastructure.Repository
} }
/// <inheritdoc/> /// <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); var result = await crudServiceBase.InsertAsync(newItem, token);
if (result > 0) if (result > 0)
@ -45,7 +46,7 @@ namespace AsbCloudInfrastructure.Repository
} }
/// <inheritdoc/> /// <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); var result = await crudServiceBase.InsertRangeAsync(dtos, token);
if (result > 0) if (result > 0)
@ -53,11 +54,21 @@ namespace AsbCloudInfrastructure.Repository
return result; 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/> /// <inheritdoc/>
public async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token) public async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
{ {
var cache = await GetCacheAsync(token); var cache = await GetCacheAsync(token);
return cache; var dtos = cache.Select(Convert);
return dtos;
} }
/// <summary> /// <summary>
@ -68,33 +79,36 @@ namespace AsbCloudInfrastructure.Repository
public TDto? GetOrDefault(int id) public TDto? GetOrDefault(int id)
{ {
var cache = GetCache(); 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/> /// <inheritdoc/>
public async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token) public async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token)
{ {
var cache = await GetCacheAsync(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/> /// <inheritdoc/>
public async Task<int> UpdateAsync(TDto dto, CancellationToken token) public virtual async Task<int> DeleteAsync(int id, 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)
{ {
var result = await crudServiceBase.DeleteAsync(id, token); var result = await crudServiceBase.DeleteAsync(id, token);
if (result > 0) if (result > 0)
DropCache(); DropCache();
return result; return result;
} }
protected virtual TDto Convert(TEntity src) => src.Adapt<TDto>();
protected virtual TEntity Convert(TDto src) => src.Adapt<TEntity>();
} }
#nullable disable #nullable disable
} }

View File

@ -27,7 +27,7 @@ namespace AsbCloudInfrastructure.Repository
var dtos = cache var dtos = cache
.Where(e => e.IdWell == idWell) .Where(e => e.IdWell == idWell)
.ToList(); .Select(Convert);
return dtos; return dtos;
} }
@ -41,7 +41,8 @@ namespace AsbCloudInfrastructure.Repository
var dtos = cache var dtos = cache
.Where(e => idsWells.Contains(e.IdWell)) .Where(e => idsWells.Contains(e.IdWell))
.ToList(); .Select(Convert);
return dtos; return dtos;
} }
} }

View File

@ -22,7 +22,8 @@ namespace AsbCloudInfrastructure.Services
.ConfigureAwait(false); .ConfigureAwait(false);
var dtos = cache var dtos = cache
.Where(f => f.Id >= 10000) .Where(f => f.Id >= 10000)
.Where(f => f.Id <= 20000); .Where(f => f.Id <= 20000)
.Select(Convert);
return dtos; return dtos;
} }

View File

@ -81,12 +81,13 @@ namespace AsbCloudInfrastructure.Services
.Select(r => r.IdWell); .Select(r => r.IdWell);
var wellsDtos = (await GetCacheAsync(token)) 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) if (dto.IdWellType is < 1 or > 2)
throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto)); throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto));
@ -112,12 +113,12 @@ namespace AsbCloudInfrastructure.Services
return result; return result;
} }
public Task<int> InsertRangeAsync(IEnumerable<WellDto> dtos, CancellationToken token) public override Task<int> InsertRangeAsync(IEnumerable<WellDto> dtos, CancellationToken token)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<int> UpdateAsync(WellDto dto, public override async Task<int> UpdateAsync(WellDto dto,
CancellationToken token = default) CancellationToken token = default)
{ {
if (dto.IdWellType is < 1 or > 2) if (dto.IdWellType is < 1 or > 2)
@ -154,9 +155,8 @@ namespace AsbCloudInfrastructure.Services
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token) public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
{ {
var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false); var entity = await GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
var dto = Convert(entity); return entity!.Caption;
return dto.Caption;
} }
public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token) public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token)