2021-12-20 15:15:20 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2022-06-06 15:43:47 +05:00
|
|
|
|
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;
|
|
|
|
|
|
2022-06-16 12:33:05 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-07-04 17:33:32 +05:00
|
|
|
|
#nullable enable
|
2022-06-06 15:43:47 +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
|
2022-06-16 12:33:05 +05:00
|
|
|
|
where TEntity : class, IId
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +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
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
public CrudCacheServiceBase(IAsbCloudDbContext dbContext)
|
|
|
|
|
: base(dbContext) { }
|
2021-12-20 15:15:20 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, ISet<string> includes)
|
|
|
|
|
: base(dbContext, includes) { }
|
2021-12-20 15:15:20 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
public CrudCacheServiceBase(IAsbCloudDbContext dbContext, Func<DbSet<TEntity>, IQueryable<TEntity>> makeQuery)
|
|
|
|
|
: base(dbContext, makeQuery) { }
|
2021-12-20 15:15:20 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override async Task<int> InsertAsync(TDto newItem, CancellationToken token)
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var result = await base.InsertAsync(newItem, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
DropCache();
|
|
|
|
|
return result;
|
2021-12-20 15:15:20 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override async Task<int> InsertRangeAsync(IEnumerable<TDto> dtos, CancellationToken token)
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +05:00
|
|
|
|
var result = await base.InsertRangeAsync(dtos, token);
|
|
|
|
|
if (result > 0)
|
|
|
|
|
DropCache();
|
|
|
|
|
return result;
|
2021-12-20 15:15:20 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override async Task<IEnumerable<TDto>> GetAllAsync(CancellationToken token)
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-16 12:33:05 +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
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +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>
|
2022-06-16 12:33:05 +05:00
|
|
|
|
public override TDto? GetOrDefault(int id)
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-16 12:33:05 +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
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
/// <inheritdoc/>
|
2022-06-16 12:33:05 +05:00
|
|
|
|
public override async Task<TDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-16 12:33:05 +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
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
/// <inheritdoc/>
|
2022-06-09 11:19:52 +05:00
|
|
|
|
public override async Task<int> UpdateAsync(TDto dto, CancellationToken token)
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-09 11:19:52 +05:00
|
|
|
|
var result = await base.UpdateAsync(dto, token);
|
2022-06-06 15:43:47 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override async Task<int> DeleteAsync(int id, CancellationToken token)
|
2021-12-20 15:15:20 +05:00
|
|
|
|
{
|
2022-06-06 15:43:47 +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)
|
2022-06-06 15:43:47 +05:00
|
|
|
|
=> GetQuery()
|
2022-11-16 17:07:47 +05:00
|
|
|
|
.FromCacheAsync(CacheTag, CacheOlescence, Convert, token);
|
2021-12-20 15:15:20 +05:00
|
|
|
|
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
2022-11-16 17:07:47 +05:00
|
|
|
|
protected virtual IEnumerable<TDto> GetCache()
|
2022-06-06 15:43:47 +05:00
|
|
|
|
=> GetQuery()
|
2022-11-16 17:07:47 +05:00
|
|
|
|
.FromCache(CacheTag, CacheOlescence, Convert);
|
2022-06-06 15:43:47 +05:00
|
|
|
|
|
|
|
|
|
protected virtual void DropCache()
|
2022-11-16 17:07:47 +05:00
|
|
|
|
=> dbSet.DropCache(CacheTag);
|
2021-12-20 15:15:20 +05:00
|
|
|
|
}
|
2022-06-06 15:43:47 +05:00
|
|
|
|
#nullable disable
|
2021-12-20 15:15:20 +05:00
|
|
|
|
}
|