Path: redb-route/redb.Route.Cache/CacheEntry.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/CacheEntry.cs
----
using System.Text.Json.Nodes;
using redb.Route.Abstractions;
namespace redb.Route.Cache;
///
/// What the cache stores for one key: the body, its content type and the headers the entry may carry.
/// A hit hands the entry to another exchange, so the headers are what the miss produced (the scope) or
/// what a put named, never what an exchange came in with and never a credential
/// ().
///
public sealed class CacheEntry
{
/// Cached body (a CLR object in the in-process cache; restored to its type from a distributed cache when possible).
public object? Body { get; init; }
/// Content type of the body.
public string? ContentType { get; init; }
/// Headers of the result message: set on In on a hit, or on the reply when .
public IReadOnlyDictionary? Headers { get; init; }
///
/// The miss left its result in exchange.Out (a request-reply producer such as HTTP replies
/// there), so a hit restores it there too — a consumer that answers only when Out is present
/// must see a hit exactly as it saw the miss.
///
public bool FromOut { get; init; }
/// With : the headers the inner steps produced on In while replying in Out; a hit sets them on In.
public IReadOnlyDictionary? InHeaders { get; init; }
/// Names of the headers the inner steps removed from In; a hit removes them as well.
public IReadOnlyCollection? RemovedHeaders { get; init; }
///
/// Captures the message with its headers as they are, credentials excluded. The scope and the
/// component keep less: the scope the headers the miss produced (), the
/// component the ones a put named.
///
public static CacheEntry From(IMessage message, bool includeHeaders, bool fromOut = false)
{
Dictionary? headers = null;
if (includeHeaders)
{
headers = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var (name, value) in message.Headers)
if (!CacheHeaderPolicy.IsCredential(name))
headers[name] = value;
}
return new()
{
Body = Capture(message),
ContentType = message.ContentType,
Headers = headers,
FromOut = fromOut,
};
}
///
/// Captures what the inner steps of a scope produced: the result message (Out when they
/// replied there, else In) and, with , the headers they added,
/// changed or removed, compared by instance with the ones the exchange came in with. A header that
/// arrived with the request is never stored; a reply that copied the request's headers keeps only
/// its own.
///
internal static CacheEntry FromResult(IExchange exchange, IReadOnlyDictionary? before)
{
var fromOut = exchange.Out is not null;
var result = fromOut ? exchange.Out! : exchange.In;
var body = Capture(result);
if (before is null)
return new() { Body = body, ContentType = result.ContentType, FromOut = fromOut };
var produced = CacheHeaderPolicy.Produced(exchange.In, before);
var removed = CacheHeaderPolicy.Removed(exchange.In, before);
return fromOut
? new()
{
Body = body,
ContentType = result.ContentType,
FromOut = true,
Headers = CacheHeaderPolicy.Produced(exchange.Out!, before),
InHeaders = produced,
RemovedHeaders = removed,
}
: new()
{
Body = body,
ContentType = result.ContentType,
Headers = produced,
RemovedHeaders = removed,
};
}
/// Captures the message for a put: the body, and of the headers only the ones named.
internal static CacheEntry FromMessage(IMessage message, IReadOnlyCollection? headerNames)
{
Dictionary? headers = null;
if (headerNames is { Count: > 0 })
{
headers = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var name in headerNames)
if (message.Headers.TryGetValue(name, out var value))
headers[name] = value;
}
return new() { Body = Capture(message), ContentType = message.ContentType, Headers = headers };
}
///
/// The body as the cache keeps it. A body can be read once, so its bytes are
/// kept and the live message continues with those bytes; a has one parent and
/// is mutable, so the cache keeps its own copy. Any other body is kept by reference — text and byte
/// arrays are treated as immutable, and a mutable object (a POCO, a list) is shared by every hit: do
/// not mutate what a cache scope handed you, or clone it first.
///
private static object? Capture(IMessage message)
{
switch (message.Body)
{
case Stream stream:
var bytes = ReadToEnd(stream);
message.Body = bytes;
return bytes;
case JsonNode node:
return node.DeepClone();
case var body:
return body;
}
}
internal static byte[] ReadToEnd(Stream stream)
{
using (stream)
{
using var buffer = new MemoryStream();
stream.CopyTo(buffer);
return buffer.ToArray();
}
}
///
/// Writes the entry into the message. Cached headers overwrite the current ones: that is what the
/// inner steps did on the miss, and a hit must leave the message the way the miss did.
///
public void ApplyTo(IMessage message)
{
message.Body = Body is JsonNode node ? node.DeepClone() : Body;
if (ContentType is not null) message.ContentType = ContentType;
if (Headers is null) return;
foreach (var (key, value) in Headers)
message.Headers[key] = value;
}
///
/// Writes the entry where the miss left it: a fresh Out when the inner steps replied there,
/// otherwise In. What the steps did to the headers of In is done again either way.
///
public void ApplyTo(IExchange exchange)
{
if (!FromOut)
{
ApplyTo(exchange.In);
RemoveFrom(exchange.In);
return;
}
if (InHeaders is not null)
foreach (var (key, value) in InHeaders)
exchange.In.Headers[key] = value;
RemoveFrom(exchange.In);
var reply = new redb.Route.Core.Message(Body);
ApplyTo(reply);
exchange.Out = reply;
}
private void RemoveFrom(IMessage message)
{
if (RemovedHeaders is null) return;
foreach (var name in RemovedHeaders)
message.Headers.Remove(name);
}
}