Files
git.stella-ops.org/src/__Libraries/StellaOps.Provcache/ProvcacheService.GetOrCompute.cs

34 lines
1017 B
C#

using System;
using System.Threading;
using System.Threading.Tasks;
namespace StellaOps.Provcache;
public sealed partial class ProvcacheService
{
/// <inheritdoc />
public async Task<ProvcacheEntry> GetOrComputeAsync(
string veriKey,
Func<CancellationToken, Task<ProvcacheEntry>> factory,
bool bypassCache = false,
CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(veriKey);
ArgumentNullException.ThrowIfNull(factory);
if (!bypassCache || !_options.AllowCacheBypass)
{
var result = await GetAsync(veriKey, bypassCache: false, cancellationToken).ConfigureAwait(false);
if (result.WasCached && result.Entry is not null)
{
return result.Entry;
}
}
var entry = await factory(cancellationToken).ConfigureAwait(false);
await SetAsync(entry, cancellationToken).ConfigureAwait(false);
return entry;
}
}