Репозиторий для кеширования
This commit is contained in:
parent
b5e255b940
commit
4d24eb9445
@ -6,43 +6,45 @@ using Persistence.Models;
|
|||||||
|
|
||||||
namespace Persistence.Repository.Repositories;
|
namespace Persistence.Repository.Repositories;
|
||||||
|
|
||||||
public class Test {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepository<TEntity, TDto>
|
public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepository<TEntity, TDto>
|
||||||
where TEntity : class, ITimestampedData, new()
|
where TEntity : class, ITimestampedData, new()
|
||||||
where TDto : class, ITimeSeriesAbstractDto, new()
|
where TDto : class, ITimeSeriesAbstractDto, new()
|
||||||
{
|
{
|
||||||
public static TDto FirstByDate { get; set; } = null!;
|
public static TDto FirstByDate { get; set; } = default!;
|
||||||
public static CyclicArray<TDto> LastData { get; set; } = null!;
|
public static CyclicArray<TDto> LastData { get; set; } = null!;
|
||||||
|
|
||||||
|
private const int CacheItemsCount = 3600;
|
||||||
|
|
||||||
public TimeSeriesDataCachedRepository(DbContext db) : base(db)
|
public TimeSeriesDataCachedRepository(DbContext db) : base(db)
|
||||||
{
|
{
|
||||||
//Task.Run(async () => {
|
Task.Run(async () =>
|
||||||
// //FirstByDate = await base.GetFirstAsync(1, CancellationToken.None);
|
{
|
||||||
// //LastData = await base.GetLastAsync(3600, CancellationToken.None);
|
LastData = new CyclicArray<TDto>(CacheItemsCount);
|
||||||
|
|
||||||
//}).wait();
|
var firstDateItem = await base.GetFirstAsync(CancellationToken.None);
|
||||||
|
if (firstDateItem == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FirstByDate = firstDateItem;
|
||||||
|
|
||||||
|
var dtos = await base.GetLastAsync(CacheItemsCount, CancellationToken.None);
|
||||||
|
dtos = dtos.OrderBy(d => d.Date);
|
||||||
|
LastData.AddRange(dtos);
|
||||||
|
}).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task<IEnumerable<TDto>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
|
public override async Task<IEnumerable<TDto>> GetAsync(DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token)
|
||||||
{
|
{
|
||||||
var cacheLastData = LastData;
|
|
||||||
|
|
||||||
if (cacheLastData[0].Date > dateBegin)
|
if (LastData.Count() == 0 || LastData[0].Date > dateBegin)
|
||||||
{
|
{
|
||||||
var dtos = await base.GetAsync(dateBegin, dateEnd, token);
|
var dtos = await base.GetAsync(dateBegin, dateEnd, token);
|
||||||
|
|
||||||
dtos = dtos.OrderBy(x => x.Date);
|
|
||||||
|
|
||||||
FirstByDate = dtos.ElementAt(0);
|
|
||||||
LastData.AddRange(dtos);
|
|
||||||
|
|
||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
var items = cacheLastData
|
var items = LastData
|
||||||
.Where(i => i.Date >= dateBegin && i.Date <= dateEnd);
|
.Where(i => i.Date >= dateBegin && i.Date <= dateEnd);
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
@ -56,7 +58,7 @@ public class TimeSeriesDataCachedRepository<TEntity, TDto> : TimeSeriesDataRepos
|
|||||||
|
|
||||||
dtos = dtos.OrderBy(x => x.Date);
|
dtos = dtos.OrderBy(x => x.Date);
|
||||||
|
|
||||||
FirstByDate = dtos.ElementAt(0);
|
FirstByDate = dtos.First();
|
||||||
LastData.AddRange(dtos);
|
LastData.AddRange(dtos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,4 +59,30 @@ public class TimeSeriesDataRepository<TEntity, TDto> : ITimeSeriesDataRepository
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<TDto>> GetLastAsync(int takeCount, CancellationToken token)
|
||||||
|
{
|
||||||
|
var query = GetQueryReadOnly()
|
||||||
|
.OrderByDescending(e => e.Date)
|
||||||
|
.Take(takeCount);
|
||||||
|
|
||||||
|
var entities = await query.ToArrayAsync(token);
|
||||||
|
var dtos = entities.Select(e => e.Adapt<TDto>());
|
||||||
|
|
||||||
|
return dtos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TDto?> GetFirstAsync(CancellationToken token)
|
||||||
|
{
|
||||||
|
var query = GetQueryReadOnly()
|
||||||
|
.OrderBy(e => e.Date);
|
||||||
|
|
||||||
|
var entity = await query.FirstOrDefaultAsync(token);
|
||||||
|
|
||||||
|
if(entity == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var dto = entity.Adapt<TDto>();
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,19 @@ public interface ITimeSeriesDataRepository<TDto> : ISyncRepository<TDto>
|
|||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token);
|
Task<int> InsertRange(IEnumerable<TDto> dtos, CancellationToken token);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получение списка последних записей
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="takeCount">количество записей</param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<TDto>> GetLastAsync(int takeCount, CancellationToken token);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получение первой записи
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<TDto?> GetFirstAsync(CancellationToken token);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user