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

50 lines
1.5 KiB
C#

using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace StellaOps.Provcache.Valkey;
public sealed partial class ValkeyProvcacheStore
{
public async ValueTask SetManyAsync(IEnumerable<ProvcacheEntry> entries, CancellationToken cancellationToken = default)
{
var entryList = entries.ToList();
cancellationToken.ThrowIfCancellationRequested();
if (entryList.Count == 0)
return;
try
{
var db = await GetDatabaseAsync(cancellationToken).ConfigureAwait(false);
var batch = db.CreateBatch();
var tasks = new List<Task>();
var now = _timeProvider.GetUtcNow();
foreach (var entry in entryList)
{
cancellationToken.ThrowIfCancellationRequested();
var redisKey = BuildKey(entry.VeriKey);
var value = JsonSerializer.Serialize(entry, _jsonOptions);
var ttl = entry.ExpiresAt - now;
if (ttl <= TimeSpan.Zero)
continue;
if (ttl > _options.MaxTtl)
ttl = _options.MaxTtl;
tasks.Add(batch.StringSetAsync(redisKey, value, ttl));
}
batch.Execute();
await Task.WhenAll(tasks).ConfigureAwait(false);
_logger.LogDebug("Batch stored {Count} cache entries", entryList.Count);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in batch cache store");
throw;
}
}
}