refactor(provcache): inject TimeProvider and IGuidProvider for determinism - DET-005
Refactored 8 files across StellaOps.Provcache, StellaOps.Provcache.Postgres, and StellaOps.Provcache.Valkey: Core Provcache library: - EvidenceChunker: Added IGuidProvider for ChunkId generation in ChunkAsync/ChunkStreamAsync - LazyFetchOrchestrator: Added IGuidProvider for ChunkId generation when storing fetched chunks - MinimalProofExporter: Added IGuidProvider for ChunkId generation in ImportAsync - FeedEpochAdvancedEvent: Added optional eventId/timestamp parameters to static Create() - SignerRevokedEvent: Added optional eventId/timestamp parameters to static Create() Postgres implementation: - PostgresProvcacheRepository: Added TimeProvider and IGuidProvider for IncrementHitCountAsync, GetStatisticsAsync, LogRevocationAsync, and MapToEntity - PostgresEvidenceChunkRepository: Added TimeProvider and IGuidProvider for GetManifestAsync and MapToEntity Valkey implementation: - ValkeyProvcacheStore: Added TimeProvider for TTL calculations in GetAsync, SetAsync, SetManyAsync All constructors use optional parameters with defaults to system implementations for backward compatibility. Added StellaOps.Determinism.Abstractions project references where needed.
This commit is contained in:
@@ -14,6 +14,7 @@ public sealed class ValkeyProvcacheStore : IProvcacheStore, IAsyncDisposable
|
||||
private readonly IConnectionMultiplexer _connectionMultiplexer;
|
||||
private readonly ProvcacheOptions _options;
|
||||
private readonly ILogger<ValkeyProvcacheStore> _logger;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
private readonly SemaphoreSlim _connectionLock = new(1, 1);
|
||||
private IDatabase? _database;
|
||||
@@ -24,11 +25,13 @@ public sealed class ValkeyProvcacheStore : IProvcacheStore, IAsyncDisposable
|
||||
public ValkeyProvcacheStore(
|
||||
IConnectionMultiplexer connectionMultiplexer,
|
||||
IOptions<ProvcacheOptions> options,
|
||||
ILogger<ValkeyProvcacheStore> logger)
|
||||
ILogger<ValkeyProvcacheStore> logger,
|
||||
TimeProvider? timeProvider = null)
|
||||
{
|
||||
_connectionMultiplexer = connectionMultiplexer ?? throw new ArgumentNullException(nameof(connectionMultiplexer));
|
||||
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_timeProvider = timeProvider ?? TimeProvider.System;
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
@@ -65,7 +68,7 @@ public sealed class ValkeyProvcacheStore : IProvcacheStore, IAsyncDisposable
|
||||
// Optionally refresh TTL on read (sliding expiration)
|
||||
if (_options.SlidingExpiration)
|
||||
{
|
||||
var ttl = entry.ExpiresAt - DateTimeOffset.UtcNow;
|
||||
var ttl = entry.ExpiresAt - _timeProvider.GetUtcNow();
|
||||
if (ttl > TimeSpan.Zero)
|
||||
{
|
||||
await db.KeyExpireAsync(redisKey, ttl).ConfigureAwait(false);
|
||||
@@ -169,7 +172,7 @@ public sealed class ValkeyProvcacheStore : IProvcacheStore, IAsyncDisposable
|
||||
var redisKey = BuildKey(entry.VeriKey);
|
||||
var value = JsonSerializer.Serialize(entry, _jsonOptions);
|
||||
|
||||
var ttl = entry.ExpiresAt - DateTimeOffset.UtcNow;
|
||||
var ttl = entry.ExpiresAt - _timeProvider.GetUtcNow();
|
||||
if (ttl <= TimeSpan.Zero)
|
||||
{
|
||||
_logger.LogDebug("Skipping expired entry for VeriKey {VeriKey}", entry.VeriKey);
|
||||
@@ -205,13 +208,14 @@ public sealed class ValkeyProvcacheStore : IProvcacheStore, IAsyncDisposable
|
||||
var db = await GetDatabaseAsync().ConfigureAwait(false);
|
||||
var batch = db.CreateBatch();
|
||||
var tasks = new List<Task>();
|
||||
var now = _timeProvider.GetUtcNow();
|
||||
|
||||
foreach (var entry in entryList)
|
||||
{
|
||||
var redisKey = BuildKey(entry.VeriKey);
|
||||
var value = JsonSerializer.Serialize(entry, _jsonOptions);
|
||||
|
||||
var ttl = entry.ExpiresAt - DateTimeOffset.UtcNow;
|
||||
var ttl = entry.ExpiresAt - now;
|
||||
if (ttl <= TimeSpan.Zero)
|
||||
continue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user