forked from ddrilling/AsbCloudServer
fix CacheTable refresh throttling
This commit is contained in:
parent
a1ea0d7cdd
commit
e1ba4e1777
3
AsbCloudDb/UsefulQueries/lost_telemetries.sql
Normal file
3
AsbCloudDb/UsefulQueries/lost_telemetries.sql
Normal 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
|
@ -14,7 +14,8 @@ namespace AsbCloudInfrastructure.Services.Cache
|
|||||||
{
|
{
|
||||||
private const int semaphoreTimeout = 5_000;
|
private const int semaphoreTimeout = 5_000;
|
||||||
private static readonly SemaphoreSlim semaphore = new(1);
|
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 readonly DbContext context;
|
||||||
private (DateTime refreshDate, IEnumerable entities) data;
|
private (DateTime refreshDate, IEnumerable entities) data;
|
||||||
@ -93,27 +94,27 @@ namespace AsbCloudInfrastructure.Services.Cache
|
|||||||
|
|
||||||
private void InternalRefresh(bool force)
|
private void InternalRefresh(bool force)
|
||||||
{
|
{
|
||||||
if (!force && (data.refreshDate + minPeriodRefresh >= DateTime.Now))
|
if (force || data.refreshDate + minPeriodRefresh > DateTime.Now)
|
||||||
return;
|
{
|
||||||
|
cached.Clear();
|
||||||
cached.Clear();
|
var entities = dbSet.AsNoTracking().ToList();
|
||||||
var entities = dbSet.AsNoTracking().ToList();
|
Trace.WriteLine($"CacheTable<{nameOfTEntity}> refresh");
|
||||||
Trace.WriteLine($"CacheTable<{typeof(TEntity).Name}> refresh");
|
cached.AddRange(entities);
|
||||||
cached.AddRange(entities);
|
data.refreshDate = DateTime.Now;
|
||||||
data.refreshDate = DateTime.Now;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task InternalRefreshAsync(bool force, CancellationToken token = default)
|
private async Task InternalRefreshAsync(bool force, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
if (!force && (data.refreshDate + minPeriodRefresh >= DateTime.Now))
|
if (force || data.refreshDate + minPeriodRefresh > DateTime.Now)
|
||||||
return;
|
{
|
||||||
|
cached.Clear();
|
||||||
cached.Clear();
|
var entities = await context.Set<TEntity>().AsNoTracking()
|
||||||
var entities = await context.Set<TEntity>().AsNoTracking()
|
.ToListAsync(token).ConfigureAwait(false);
|
||||||
.ToListAsync(token).ConfigureAwait(false);
|
Trace.WriteLine($"CacheTable<{nameOfTEntity}> refreshAsync");
|
||||||
Trace.WriteLine($"CacheTable<{typeof(TEntity).Name}> refresh");
|
cached.AddRange(entities);
|
||||||
cached.AddRange(entities);
|
data.refreshDate = DateTime.Now;
|
||||||
data.refreshDate = DateTime.Now;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Refresh(bool force)
|
public int Refresh(bool force)
|
||||||
|
Loading…
Reference in New Issue
Block a user