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 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)
|
||||
|
Loading…
Reference in New Issue
Block a user