forked from ddrilling/AsbCloudServer
140 lines
4.6 KiB
C#
140 lines
4.6 KiB
C#
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
|
|
/// <summary>
|
|
/// CRUD ñåðâèñ ñ êåøåì â îïåðàòèâêå
|
|
/// </summary>
|
|
/// <typeparam name="TDto"></typeparam>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
public class CrudCacheServiceBase<TDto, TEntity> : CrudServiceBase<TDto, TEntity>
|
|
where TDto : AsbCloudApp.Data.IId
|
|
where TEntity : class, IId
|
|
{
|
|
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, IMemoryCache memoryCache)
|
|
: base(dbContext)
|
|
{
|
|
this.memoryCache = memoryCache;
|
|
}
|
|
|
|
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)
|
|
{
|
|
var result = await base.InsertAsync(newItem, token);
|
|
if (result > 0)
|
|
DropCache();
|
|
return result;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
|
{
|
|
var result = await base.InsertRangeAsync(dtos, token);
|
|
if (result > 0)
|
|
DropCache();
|
|
return result;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
|
|
{
|
|
var cache = await GetCacheAsync(token);
|
|
return cache;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ñèíõðîííî ïîëó÷èòü çàïèñü ïî ÈÄ
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public override TDto? GetOrDefault(int id)
|
|
{
|
|
var cache = GetCache();
|
|
return cache.FirstOrDefault(d => d.Id == id);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
|
{
|
|
var cache = await GetCacheAsync(token);
|
|
return cache.FirstOrDefault(d => d.Id == id);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override async Task<int> UpdateAsync(TDto dto, CancellationToken token)
|
|
{
|
|
var result = await base.UpdateAsync(dto, token);
|
|
if (result > 0)
|
|
DropCache();
|
|
return result;
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
var result = await base.DeleteAsync(id, token);
|
|
if (result > 0)
|
|
DropCache();
|
|
return result;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
#nullable disable
|
|
} |