DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/CrudCacheServiceBase.cs

140 lines
4.6 KiB
C#
Raw Normal View History

2021-12-20 15:15:20 +05:00
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
2021-12-20 15:15:20 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
2021-12-20 15:15:20 +05:00
{
2022-07-04 17:33:32 +05:00
#nullable enable
/// <summary>
/// CRUD <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TEntity"></typeparam>
2022-06-15 14:57:37 +05:00
public class CrudCacheServiceBase<TDto, TEntity> : CrudServiceBase<TDto, TEntity>
2021-12-20 15:15:20 +05:00
where TDto : AsbCloudApp.Data.IId
where TEntity : class, IId
2021-12-20 15:15:20 +05:00
{
protected string CacheTag = typeof(TDto).Name;
protected TimeSpan CacheOlescence = TimeSpan.FromMinutes(5);
private readonly IMemoryCache memoryCache;
2021-12-20 15:15:20 +05:00
protected int KeySelector(TEntity entity) => entity.Id;
2021-12-20 15:15:20 +05:00
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
: base(dbContext)
{
this.memoryCache = memoryCache;
}
2021-12-20 15:15:20 +05:00
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, IMemoryCache memoryCache, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
: base(dbContext, makeQuery)
{
this.memoryCache = memoryCache;
}
2021-12-20 15:15:20 +05:00
/// <inheritdoc/>
public override async Task<int> InsertAsync(TDto newItem, CancellationToken token)
2021-12-20 15:15:20 +05:00
{
var result = await base.InsertAsync(newItem, token);
if (result > 0)
DropCache();
return result;
2021-12-20 15:15:20 +05:00
}
/// <inheritdoc/>
public override async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
2021-12-20 15:15:20 +05:00
{
var result = await base.InsertRangeAsync(dtos, token);
if (result > 0)
DropCache();
return result;
2021-12-20 15:15:20 +05:00
}
/// <inheritdoc/>
public override async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
2021-12-20 15:15:20 +05:00
{
var cache = await GetCacheAsync(token);
2022-11-16 17:07:47 +05:00
return cache;
2021-12-20 15:15:20 +05:00
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override TDto? GetOrDefault(int id)
2021-12-20 15:15:20 +05:00
{
var cache = GetCache();
2022-11-16 17:07:47 +05:00
return cache.FirstOrDefault(d => d.Id == id);
2021-12-20 15:15:20 +05:00
}
/// <inheritdoc/>
public override async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token)
2021-12-20 15:15:20 +05:00
{
var cache = await GetCacheAsync(token);
2022-11-16 17:07:47 +05:00
return cache.FirstOrDefault(d => d.Id == id);
2021-12-20 15:15:20 +05:00
}
/// <inheritdoc/>
public override async Task<int> UpdateAsync(TDto dto, CancellationToken token)
2021-12-20 15:15:20 +05:00
{
var result = await base.UpdateAsync(dto, token);
if (result > 0)
DropCache();
return result;
2021-12-20 15:15:20 +05:00
}
2022-06-15 14:57:37 +05:00
/// <inheritdoc/>
public override async Task<int> UpdateRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
{
var result = await base.UpdateRangeAsync(dtos, token);
if (result > 0)
DropCache();
return result;
}
/// <inheritdoc/>
public override async Task<int> DeleteAsync(int id, CancellationToken token)
2021-12-20 15:15:20 +05:00
{
var result = await base.DeleteAsync(id, token);
if (result > 0)
DropCache();
return result;
2021-12-20 15:15:20 +05:00
}
2022-11-16 17:07:47 +05:00
protected virtual Task<IEnumerable<TDto>> GetCacheAsync(CancellationToken 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;
}
2022-11-16 17:07:47 +05:00
protected virtual IEnumerable<TDto> GetCache()
{
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()
=> memoryCache.Remove(CacheTag);
2021-12-20 15:15:20 +05:00
}
#nullable disable
2021-12-20 15:15:20 +05:00
}