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

117 lines
3.8 KiB
C#
Raw Normal View History

2021-12-20 15:15:20 +05:00
using AsbCloudDb.Model;
using AsbCloudInfrastructure.EfCache;
using Microsoft.EntityFrameworkCore;
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
{
/// <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);
protected int KeySelector(TEntity entity) => entity.Id;
2021-12-20 15:15:20 +05:00
public CrudCacheServiceBase(IAsbCloudDbContext dbContext)
: base(dbContext) { }
2021-12-20 15:15:20 +05:00
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
: base(dbContext, includes) { }
2021-12-20 15:15:20 +05:00
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
: base(dbContext, makeQuery) { }
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);
return cache.Values;
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();
return cache.GetValueOrDefault(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);
return cache.GetValueOrDefault(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
}
protected virtual Task<Dictionary<int, TDto>> GetCacheAsync(CancellationToken token)
=> GetQuery()
.FromCacheDictionaryAsync(CacheTag, CacheOlescence, KeySelector, Convert, token);
2021-12-20 15:15:20 +05:00
protected virtual Dictionary<int, TDto> GetCache()
=> GetQuery()
.FromCacheDictionary(CacheTag, CacheOlescence, KeySelector, Convert);
protected virtual void DropCache()
=> dbSet.DropCacheDictionary(CacheTag);
2021-12-20 15:15:20 +05:00
}
#nullable disable
2021-12-20 15:15:20 +05:00
}