Guides & Recipes — Caching
L2StableNuGet: v1.0.0Local HEAD: v2.0.0AOT: Full
Recommended production patterns for multi-tier caching and trimming-safe JSON serialization.
Recipe 1: Composing HybridCacheProvider with Different L1/L2 TTLs
Section titled “Recipe 1: Composing HybridCacheProvider with Different L1/L2 TTLs”Use short local L1 durations (e.g., 30 seconds) paired with longer distributed L2 Redis durations (e.g., 10 minutes) via HybridCacheEntryOptions.FromDurations(TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(10)). Set failOpenOnRemoval: true only when transient L2 removal failures should not abort writes.
Recipe 2: Batch Reads and Tag-Based Eviction
Section titled “Recipe 2: Batch Reads and Tag-Based Eviction”- Use
GetManyAsync<T>(keys, ct)andSetManyAsync<T>(entries, options, ct)to reduce round trips across Redis pipelines. - Tag related projections (e.g.,
["catalog", $"category:{id}"]) and callRemoveByTagAsync("catalog", ct)onITaggedCacheProviderinstead of scanning prefixes on hot paths.
Recipe 3: Native AOT JsonSerializerContext
Section titled “Recipe 3: Native AOT JsonSerializerContext”Instantiate new SystemTextJsonCacheSerializer(AppJsonContext.Default.Options) with your [JsonSerializable] context rather than SystemTextJsonCacheSerializer.Default so trimming does not remove required DTO metadata.