Don't schedule maintenance for ArchiCacheable objects

This should be upper level logic, if somebody needs it.
This commit is contained in:
JustArchi
2019-10-11 18:45:53 +02:00
parent 0c9f4004c2
commit 9551448f56

View File

@@ -35,25 +35,15 @@ namespace ArchiSteamFarm.Helpers {
private bool IsPermanentCache => CacheLifetime == Timeout.InfiniteTimeSpan;
private bool IsRecent => IsPermanentCache || (DateTime.UtcNow.Subtract(InitializedAt) < CacheLifetime);
// Purge should happen slightly after lifetime, to allow eventual refresh if the property is still used
private TimeSpan PurgeLifetime => CacheLifetime + TimeSpan.FromMinutes(5);
private DateTime InitializedAt;
private T InitializedValue;
private Timer MaintenanceTimer;
public ArchiCacheable([NotNull] Func<Task<(bool Success, T Result)>> resolveFunction, TimeSpan? cacheLifetime = null) {
ResolveFunction = resolveFunction ?? throw new ArgumentNullException(nameof(resolveFunction));
CacheLifetime = cacheLifetime ?? Timeout.InfiniteTimeSpan;
}
public void Dispose() {
// Those are objects that are always being created if constructor doesn't throw exception
InitSemaphore.Dispose();
// Those are objects that might be null and the check should be in-place
MaintenanceTimer?.Dispose();
}
public void Dispose() => InitSemaphore.Dispose();
[PublicAPI]
public async Task<(bool Success, T Result)> GetValue(EFallback fallback = EFallback.DefaultForType) {
@@ -94,19 +84,6 @@ namespace ArchiSteamFarm.Helpers {
InitializedValue = result;
InitializedAt = DateTime.UtcNow;
if (!IsPermanentCache) {
if (MaintenanceTimer == null) {
MaintenanceTimer = new Timer(
async e => await SoftReset().ConfigureAwait(false),
null,
PurgeLifetime, // Delay
Timeout.InfiniteTimeSpan // Period
);
} else {
MaintenanceTimer.Change(PurgeLifetime, Timeout.InfiniteTimeSpan);
}
}
return (true, result);
} finally {
InitSemaphore.Release();
@@ -126,38 +103,8 @@ namespace ArchiSteamFarm.Helpers {
return;
}
HardReset();
} finally {
InitSemaphore.Release();
}
}
private void HardReset(bool withValue = true) {
InitializedAt = DateTime.MinValue;
if (withValue) {
InitializedAt = DateTime.MinValue;
InitializedValue = default;
}
if (MaintenanceTimer != null) {
MaintenanceTimer.Dispose();
MaintenanceTimer = null;
}
}
private async Task SoftReset() {
if (!IsInitialized || IsRecent) {
return;
}
await InitSemaphore.WaitAsync().ConfigureAwait(false);
try {
if (!IsInitialized || IsRecent) {
return;
}
HardReset(false);
} finally {
InitSemaphore.Release();
}