From 9551448f56ff70d5d1659b41e8df51b50e2df9da Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 11 Oct 2019 18:45:53 +0200 Subject: [PATCH] Don't schedule maintenance for ArchiCacheable objects This should be upper level logic, if somebody needs it. --- ArchiSteamFarm/Helpers/ArchiCacheable.cs | 57 +----------------------- 1 file changed, 2 insertions(+), 55 deletions(-) diff --git a/ArchiSteamFarm/Helpers/ArchiCacheable.cs b/ArchiSteamFarm/Helpers/ArchiCacheable.cs index 485d4603b..a46f37e2e 100644 --- a/ArchiSteamFarm/Helpers/ArchiCacheable.cs +++ b/ArchiSteamFarm/Helpers/ArchiCacheable.cs @@ -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> 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(); }