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

40 lines
1.3 KiB
C#

using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace StellaOps.Provcache.Valkey;
public sealed partial class ValkeyProvcacheStore
{
public async ValueTask SetAsync(ProvcacheEntry entry, CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
var db = await GetDatabaseAsync(cancellationToken).ConfigureAwait(false);
var redisKey = BuildKey(entry.VeriKey);
var value = JsonSerializer.Serialize(entry, _jsonOptions);
var ttl = entry.ExpiresAt - _timeProvider.GetUtcNow();
if (ttl <= TimeSpan.Zero)
{
_logger.LogDebug("Skipping expired entry for VeriKey {VeriKey}", entry.VeriKey);
return;
}
if (ttl > _options.MaxTtl)
{
ttl = _options.MaxTtl;
}
await db.StringSetAsync(redisKey, value, ttl).ConfigureAwait(false);
_logger.LogDebug("Stored cache entry for VeriKey {VeriKey} with TTL {Ttl}", entry.VeriKey, ttl);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error storing cache entry for VeriKey {VeriKey}", entry.VeriKey);
throw;
}
}
}