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

48 lines
1.6 KiB
C#

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace StellaOps.Provcache;
public sealed partial class ProvcacheService
{
/// <inheritdoc />
public async Task<bool> SetAsync(ProvcacheEntry entry, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entry);
var sw = Stopwatch.StartNew();
using var activity = ProvcacheTelemetry.StartSetActivity(entry.VeriKey, entry.Decision.TrustScore);
try
{
await _store.SetAsync(entry, cancellationToken).ConfigureAwait(false);
if (_options.EnableWriteBehind && _writeBehindQueue is not null)
{
await _writeBehindQueue.EnqueueAsync(entry, cancellationToken).ConfigureAwait(false);
}
else
{
await _repository.UpsertAsync(entry, cancellationToken).ConfigureAwait(false);
}
sw.Stop();
ProvcacheTelemetry.RecordRequest("set", ProvcacheTelemetry.ResultCreated);
ProvcacheTelemetry.RecordLatency("set", sw.Elapsed);
_logger.LogDebug("Stored cache entry for VeriKey {VeriKey}", entry.VeriKey);
return true;
}
catch (Exception ex)
{
ProvcacheTelemetry.MarkError(activity, ex.Message);
ProvcacheTelemetry.RecordRequest("set", ProvcacheTelemetry.ResultError);
_logger.LogError(ex, "Error storing cache entry for VeriKey {VeriKey}", entry.VeriKey);
return false;
}
}
}