2018-12-16 01:08:09 +03:00
|
|
|
// _ _ _ ____ _ _____
|
2018-12-12 22:19:52 +01:00
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2020-02-01 23:33:35 +01:00
|
|
|
// Copyright 2015-2020 Łukasz "JustArchi" Domeradzki
|
2018-12-12 22:19:52 +01:00
|
|
|
// Contact: JustArchi@JustArchi.net
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2018-12-12 22:19:52 +01:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2018-12-12 22:19:52 +01:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2019-01-14 19:11:17 +01:00
|
|
|
// |
|
2018-12-12 22:19:52 +01:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-11-11 16:51:31 +01:00
|
|
|
using System.Globalization;
|
2018-12-12 22:19:52 +01:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-01-21 22:41:03 +01:00
|
|
|
using ArchiSteamFarm.Localization;
|
2019-01-10 22:33:07 +01:00
|
|
|
using JetBrains.Annotations;
|
2018-12-12 22:19:52 +01:00
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm.Helpers {
|
2020-08-22 21:41:01 +02:00
|
|
|
public sealed class ArchiCacheable<T> : IDisposable where T : class {
|
2018-12-12 22:19:52 +01:00
|
|
|
private readonly TimeSpan CacheLifetime;
|
2020-11-14 22:37:00 +01:00
|
|
|
private readonly SemaphoreSlim InitSemaphore = new(1, 1);
|
2020-08-22 21:41:01 +02:00
|
|
|
private readonly Func<Task<(bool Success, T? Result)>> ResolveFunction;
|
2018-12-12 22:19:52 +01:00
|
|
|
|
|
|
|
|
private bool IsInitialized => InitializedAt > DateTime.MinValue;
|
|
|
|
|
private bool IsPermanentCache => CacheLifetime == Timeout.InfiniteTimeSpan;
|
|
|
|
|
private bool IsRecent => IsPermanentCache || (DateTime.UtcNow.Subtract(InitializedAt) < CacheLifetime);
|
|
|
|
|
|
|
|
|
|
private DateTime InitializedAt;
|
2020-08-22 21:41:01 +02:00
|
|
|
private T? InitializedValue;
|
2018-12-12 22:19:52 +01:00
|
|
|
|
2020-08-22 21:41:01 +02:00
|
|
|
public ArchiCacheable(Func<Task<(bool Success, T? Result)>> resolveFunction, TimeSpan? cacheLifetime = null) {
|
2018-12-12 22:19:52 +01:00
|
|
|
ResolveFunction = resolveFunction ?? throw new ArgumentNullException(nameof(resolveFunction));
|
|
|
|
|
CacheLifetime = cacheLifetime ?? Timeout.InfiniteTimeSpan;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 18:45:53 +02:00
|
|
|
public void Dispose() => InitSemaphore.Dispose();
|
2018-12-12 22:19:52 +01:00
|
|
|
|
2019-01-10 22:33:07 +01:00
|
|
|
[PublicAPI]
|
2020-08-22 21:41:01 +02:00
|
|
|
public async Task<(bool Success, T? Result)> GetValue(EFallback fallback = EFallback.DefaultForType) {
|
2019-01-21 22:41:03 +01:00
|
|
|
if (!Enum.IsDefined(typeof(EFallback), fallback)) {
|
2020-08-22 21:41:01 +02:00
|
|
|
throw new ArgumentNullException(nameof(fallback));
|
2019-01-21 22:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 22:19:52 +01:00
|
|
|
if (IsInitialized && IsRecent) {
|
|
|
|
|
return (true, InitializedValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await InitSemaphore.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (IsInitialized && IsRecent) {
|
|
|
|
|
return (true, InitializedValue);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 21:41:01 +02:00
|
|
|
(bool success, T? result) = await ResolveFunction().ConfigureAwait(false);
|
2018-12-15 00:27:15 +01:00
|
|
|
|
2018-12-12 22:19:52 +01:00
|
|
|
if (!success) {
|
2019-01-21 22:41:03 +01:00
|
|
|
switch (fallback) {
|
|
|
|
|
case EFallback.DefaultForType:
|
2020-11-14 22:37:00 +01:00
|
|
|
return (false, default(T?));
|
2019-01-21 22:41:03 +01:00
|
|
|
case EFallback.FailedNow:
|
|
|
|
|
return (false, result);
|
|
|
|
|
case EFallback.SuccessPreviously:
|
|
|
|
|
return (false, InitializedValue);
|
|
|
|
|
default:
|
2020-11-11 16:51:31 +01:00
|
|
|
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnknownValuePleaseReport, nameof(fallback), fallback));
|
2019-05-19 15:38:06 +02:00
|
|
|
|
2019-01-21 22:41:03 +01:00
|
|
|
goto case EFallback.DefaultForType;
|
|
|
|
|
}
|
2018-12-12 22:19:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitializedValue = result;
|
|
|
|
|
InitializedAt = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
return (true, result);
|
|
|
|
|
} finally {
|
|
|
|
|
InitSemaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 22:41:03 +01:00
|
|
|
[PublicAPI]
|
|
|
|
|
public async Task Reset() {
|
2018-12-12 22:19:52 +01:00
|
|
|
if (!IsInitialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await InitSemaphore.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!IsInitialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 18:45:53 +02:00
|
|
|
InitializedAt = DateTime.MinValue;
|
2020-11-14 22:37:00 +01:00
|
|
|
InitializedValue = default(T?);
|
2018-12-12 22:19:52 +01:00
|
|
|
} finally {
|
|
|
|
|
InitSemaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-21 22:41:03 +01:00
|
|
|
|
|
|
|
|
public enum EFallback : byte {
|
|
|
|
|
DefaultForType,
|
|
|
|
|
FailedNow,
|
|
|
|
|
SuccessPreviously
|
|
|
|
|
}
|
2018-12-12 22:19:52 +01:00
|
|
|
}
|
|
|
|
|
}
|