Path: redb-route/redb.Route.Cache/CacheScopeProcessor.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Cache/CacheScopeProcessor.cs ---- using System.Collections.Concurrent; using redb.Route.Abstractions; namespace redb.Route.Cache; /// Runtime side of . internal sealed class CacheScopeProcessor : IProcessor { /// Header set on every exchange passing a cache node: true on a hit, false on a miss. public const string HitHeader = "cache.hit"; private readonly ICacheStore _store; // Single flight per key: while one exchange computes a missing entry, the others arriving for the // same key wait for its result instead of each running the inner steps (a stampede on a hot key // the moment it expires is exactly what a cache is there to prevent). The map belongs to the // store's context, not to this node: two nodes on one region and key are one flight. private readonly ConcurrentDictionary> _inFlight; private readonly Func _key; private readonly TimeSpan? _ttl; private readonly TimeSpan? _sliding; private readonly bool _cacheHeaders; private readonly IProcessor _inner; public CacheScopeProcessor( ICacheStore store, ConcurrentDictionary> inFlight, Func key, TimeSpan? ttl, TimeSpan? sliding, bool cacheHeaders, IProcessor inner) { _store = store; _inFlight = inFlight; _key = key; _ttl = ttl; _sliding = sliding; _cacheHeaders = cacheHeaders; _inner = inner; } public async Task Process(IExchange exchange, CancellationToken ct = default) { var key = _key(exchange); while (true) { var hit = await _store.GetAsync(key, ct).ConfigureAwait(false); if (hit is not null) { Apply(exchange, hit); return; } var mine = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var owner = _inFlight.GetOrAdd(key, mine); if (!ReferenceEquals(owner, mine)) { // Somebody else is computing this key: take its result. Null means it failed or stopped // without caching; then look again, and compute here if nobody else has taken over. var computed = await owner.Task.WaitAsync(ct).ConfigureAwait(false); if (computed is not null) { Apply(exchange, computed); return; } continue; } try { var entry = await ComputeAsync(exchange, key, ct).ConfigureAwait(false); mine.TrySetResult(entry); return; } catch { mine.TrySetResult(null); throw; } finally { _inFlight.TryRemove(new KeyValuePair>(key, mine)); } } } private static void Apply(IExchange exchange, CacheEntry entry) { entry.ApplyTo(exchange); exchange.In.Headers[HitHeader] = true; } /// Runs the inner steps and stores the result; null when the exchange stopped or failed (nothing cached). private async Task ComputeAsync(IExchange exchange, string key, CancellationToken ct) { exchange.In.Headers[HitHeader] = false; // With CacheHeaders(): the headers the exchange came in with, so the entry can keep what the // inner steps did to them and nothing of what arrived (another exchange gets the entry on a hit). var before = _cacheHeaders ? CacheHeaderPolicy.Snapshot(exchange.In) : null; await _inner.Process(exchange, ct).ConfigureAwait(false); if (exchange.IsStopped || exchange.Exception is not null) return null; var entry = CacheEntry.FromResult(exchange, before); await _store.SetAsync(key, entry, _ttl, _sliding, ct).ConfigureAwait(false); return entry; } }