Skip to content

CachingBehavior<TMessage, TResult> class

Namespace: Vitreous.Caching.Behaviors
Assembly: Vitreous.Caching.dll

Pipeline behavior that short-circuits query execution on a cache hit. Only active for messages that implement ICacheableQuery<TResult>; all other messages pass through unchanged.

public sealed class CachingBehavior<TMessage, TResult> : IPipelineBehavior<TMessage, TResult>

TMessage

TResult

object ← CachingBehavior<TMessage, TResult>

IPipelineBehavior<TMessage, TResult>

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Register this as the innermost behavior (closest to the handler) so outer behaviors such as logging and tracing still execute on cache hits. On a hit the handler is never called. On a miss the result is cached only when IsSuccess is true. Cache failures (e.g. Redis unavailable) are logged as warnings and the handler is called as a fallback — the cache is always a performance layer, never a hard dependency.

With [EnableStampedeProtection](/Vitreous/api/vitreous/caching/vitreouscachingoptions/#Vitreous_Caching_VitreousCachingOptions_EnableStampedeProtection) on (the default), concurrent misses for the same key share one handler run. If the caller running the handler is canceled, the others run it themselves.

CachingBehavior(IVitreousCache, ILogger<CachingBehavior<TMessage, TResult>>)

Section titled “ CachingBehavior(IVitreousCache, ILogger<CachingBehavior<TMessage, TResult>>)”
public CachingBehavior(IVitreousCache cache, ILogger<CachingBehavior<TMessage, TResult>> logger)

cache IVitreousCache

logger ILogger<CachingBehavior<TMessage, TResult>>

CachingBehavior(IVitreousCache, ILogger<CachingBehavior<TMessage, TResult>>, VitreousCachingOptions?)

Section titled “ CachingBehavior(IVitreousCache, ILogger<CachingBehavior<TMessage, TResult>>, VitreousCachingOptions?)”
public CachingBehavior(IVitreousCache cache, ILogger<CachingBehavior<TMessage, TResult>> logger, VitreousCachingOptions? options)

cache IVitreousCache

logger ILogger<CachingBehavior<TMessage, TResult>>

options VitreousCachingOptions?

Handle(TMessage, MessageHandlerDelegate<TMessage, TResult>, CancellationToken)

Section titled “ Handle(TMessage, MessageHandlerDelegate<TMessage, TResult>, CancellationToken)”

Executes this behavior step, calling next to continue the pipeline.

public ValueTask<Result<TResult>> Handle(TMessage message, MessageHandlerDelegate<TMessage, TResult> next, CancellationToken ct = default)

message TMessage

next MessageHandlerDelegate<TMessage, TResult>

ct CancellationToken

ValueTask<Result<TResult>>