HttpClientFactory caching sample
This sample demonstrates HttpHybridCacheHandler with IHttpClientFactory and
Microsoft dependency injection, the same registration pattern used in ASP.NET Core.
Overview
The sample shows:
- Registering HybridCache and the caching handler through dependency injection.
- Configuring a named
HttpClientwith the handler. - Making requests that benefit from client-side caching.
- Observing cache hits through timing differences.
Run the sample
From the repository root:
dotnet run --project hybrid-cache-handler\samples\HttpClientFactorySample\HttpClientFactorySample.csprojThe console application makes three identical requests to
https://httpbin.org/cache/60 and waits for a key before exiting. The intended flow:
- First request: full round-trip to the origin.
- Second request: served from cache, normally faster.
- Third request: still served from cache.
The sample prints elapsed time, status, the first response's Cache-Control, and later responses' Age headers. These are demonstration timings, not benchmarks; origin availability and cache directives determine the actual results.
The former README referred to the GitHub API; the current program uses httpbin.
Configuration
- FallbackCacheDuration: Explicitly set to five minutes for responses without caching headers.
- MaxCacheableContentSize: Uses the default 10 MB maximum.
- CompressionThreshold: 1024 bytes.
- Transport:
SocketsHttpHandlerwith automatic decompression, a five-minute pooled connection lifetime, and a two-minute idle timeout.
The former README's HybridCacheHttpHandler, HybridCacheHttpHandlerOptions,
and DefaultCacheDuration names are outdated. Current names are
HttpHybridCacheHandler, HttpHybridCacheHandlerOptions, and
FallbackCacheDuration.
Key code
The current sample registers the handler through its service helper:
builder.Services.AddHttpHybridCacheHandler(options =>
{
options.FallbackCacheDuration = TimeSpan.FromMinutes(5);
options.CompressionThreshold = 1024;
});
builder.Services.AddHttpClient("CachedClient")
.ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
{
AutomaticDecompression = DecompressionMethods.All,
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2)
})
.AddHttpMessageHandler(sp => sp.GetRequiredService<HttpHybridCacheHandler>());See the source for imports and host setup.