fix CacheTable refresh throttling

This commit is contained in:
Фролов 2021-11-10 17:52:28 +05:00
parent a1ea0d7cdd
commit e1ba4e1777
2 changed files with 22 additions and 18 deletions

View File

@ -0,0 +1,3 @@
SELECT * from t_telemetry
left outer join t_well on t_telemetry.id = t_well.id_telemetry
where t_well.id is null

View File

@ -14,7 +14,8 @@ namespace AsbCloudInfrastructure.Services.Cache
{
private const int semaphoreTimeout = 5_000;
private static readonly SemaphoreSlim semaphore = new(1);
private static readonly TimeSpan minPeriodRefresh = TimeSpan.FromSeconds(3);
private static readonly TimeSpan minPeriodRefresh = TimeSpan.FromSeconds(5);
private static readonly string nameOfTEntity = typeof(TEntity).Name;
private readonly DbContext context;
private (DateTime refreshDate, IEnumerable entities) data;
@ -93,27 +94,27 @@ namespace AsbCloudInfrastructure.Services.Cache
private void InternalRefresh(bool force)
{
if (!force && (data.refreshDate + minPeriodRefresh >= DateTime.Now))
return;
cached.Clear();
var entities = dbSet.AsNoTracking().ToList();
Trace.WriteLine($"CacheTable<{typeof(TEntity).Name}> refresh");
cached.AddRange(entities);
data.refreshDate = DateTime.Now;
if (force || data.refreshDate + minPeriodRefresh > DateTime.Now)
{
cached.Clear();
var entities = dbSet.AsNoTracking().ToList();
Trace.WriteLine($"CacheTable<{nameOfTEntity}> refresh");
cached.AddRange(entities);
data.refreshDate = DateTime.Now;
}
}
private async Task InternalRefreshAsync(bool force, CancellationToken token = default)
{
if (!force && (data.refreshDate + minPeriodRefresh >= DateTime.Now))
return;
cached.Clear();
var entities = await context.Set<TEntity>().AsNoTracking()
.ToListAsync(token).ConfigureAwait(false);
Trace.WriteLine($"CacheTable<{typeof(TEntity).Name}> refresh");
cached.AddRange(entities);
data.refreshDate = DateTime.Now;
if (force || data.refreshDate + minPeriodRefresh > DateTime.Now)
{
cached.Clear();
var entities = await context.Set<TEntity>().AsNoTracking()
.ToListAsync(token).ConfigureAwait(false);
Trace.WriteLine($"CacheTable<{nameOfTEntity}> refreshAsync");
cached.AddRange(entities);
data.refreshDate = DateTime.Now;
}
}
public int Refresh(bool force)