using AsbCloudApp.Services; using AsbCloudDb.Model; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Repository { #nullable enable /// /// CRUD сервис с кешем в оперативке /// /// /// public class CrudCacheRepositoryBase : CacheBase, ICrudRepository where TDto : AsbCloudApp.Data.IId where TEntity : class, IId { protected int KeySelector(TEntity entity) => entity.Id; protected readonly ICrudRepository crudServiceBase; public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache) : base(dbContext, memoryCache) { crudServiceBase = new CrudRepositoryBase(dbContext); } public CrudCacheRepositoryBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, Func, IQueryable> makeQuery) : base(dbContext, memoryCache, makeQuery) { crudServiceBase = new CrudRepositoryBase(dbContext, makeQuery); } /// public async Task InsertAsync(TDto newItem, CancellationToken token) { var result = await crudServiceBase.InsertAsync(newItem, token); if (result > 0) DropCache(); return result; } /// public async Task InsertRangeAsync(IEnumerable dtos, CancellationToken token) { var result = await crudServiceBase.InsertRangeAsync(dtos, token); if (result > 0) DropCache(); return result; } /// public async Task> GetAllAsync(CancellationToken token) { var cache = await GetCacheAsync(token); return cache; } /// /// Синхронно получить запись по ИД /// /// /// public TDto? GetOrDefault(int id) { var cache = GetCache(); return cache.FirstOrDefault(d => d.Id == id); } /// public async Task GetOrDefaultAsync(int id, CancellationToken token) { var cache = await GetCacheAsync(token); return cache.FirstOrDefault(d => d.Id == id); } /// public async Task UpdateAsync(TDto dto, CancellationToken token) { var result = await crudServiceBase.UpdateAsync(dto, token); if (result > 0) DropCache(); return result; } /// public async Task DeleteAsync(int id, CancellationToken token) { var result = await crudServiceBase.DeleteAsync(id, token); if (result > 0) DropCache(); return result; } } #nullable disable }