Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

Replicant

> 开发工具
Open source

A wrapper for HttpClient that caches to disk. Cached files, over the max specified, are deleted based on the last access times.

598 stars0 likes1 views
WebsiteGitHub

About

A wrapper for HttpClient that caches to disk. Cached files, over the max specified, are deleted based on the last access times.

Replicant

A wrapper for HttpClient that caches to disk. Cached files, over the max specified, are deleted based on the last access times.

See Milestones for release notes.

Headers/Responses respected in caching decisions:

  • Expires
  • Cache-Control max-age
  • Cache-Control no-store
  • Cache-Control no-cache
  • Last-Modified
  • ETag

NuGet package

https://nuget.org/packages/Replicant/

Usage

Default instance

There is a default static instance:

cs
var content = await HttpCache.Default.DownloadAsync("https://httpbin.org/status/200");

snippet source | anchor

This caches to {Temp}/Replicant.

Construction

An instance of HttpCache should be long running.

cs
var httpCache = new HttpCache(
    cacheDirectory,
    // omit for default new HttpClient()
    new HttpClient
    {
        Timeout = TimeSpan.FromSeconds(30)
    },
    // omit for the default of 1000
    maxEntries: 10000);

// Dispose when finished
await httpCache.DisposeAsync();

snippet source | anchor

Dependency injection

Add HttpCache as a singleton when using dependency injection.

cs
var services = new ServiceCollection();
services.AddSingleton(_ => new HttpCache(cacheDirectory));

using var provider = services.BuildServiceProvider();
var httpCache = provider.GetRequiredService();
NotNull(httpCache);

snippet source | anchor

Using HttpClient with HttpClientFactory.

cs
var services = new ServiceCollection();
services.AddHttpClient();
services.AddSingleton(_ =>
{
    var clientFactory = _.GetRequiredService();
    return new HttpCache(cacheDirectory, clientFactory.CreateClient);
});

using var provider = services.BuildServiceProvider();
var httpCache = provider.GetRequiredService();
NotNull(httpCache);

snippet source | anchor

DelegatingHandler

ReplicantHandler can be used as a DelegatingHandler in the HttpClient pipeline:

cs
var handler = new ReplicantHandler(cacheDirectory)
{
    InnerHandler = new HttpClientHandler()
};
using var client = new HttpClient(handler);
var response = await client.GetAsync("https://example.com");

snippet source | anchor

DelegatingHandler with HttpClientFactory

ReplicantHandler integrates with HttpClientFactory using AddHttpMessageHandler:

cs
var services = new ServiceCollection();
services.AddHttpClient("CachedClient")
    .AddHttpMessageHandler(() => new ReplicantHandler(cacheDirectory));

snippet source | anchor

To share a single cache (and purge timer) across multiple named clients, register a ReplicantCache as a singleton:

cs
var services = new ServiceCollection();
services.AddReplicantCache(cacheDirectory);
services.AddHttpClient("CachedClient")
    .AddReplicantCaching();

snippet source | anchor

Composing with Microsoft.Extensions.Http.Resilience

ReplicantHandler is a standard DelegatingHandler, so it composes with the Microsoft.Extensions.Http.Resilience package (retry / circuit-breaker / timeout strategies built on Polly).

Do you actually need it? Replicant already ships with built-in retry-with-exponential-backoff via the maxRetries parameter on both HttpCache and ReplicantHandler — see Retry on transient failure. If retries on transient 5xx / 408 / network exceptions are all you need, use that and skip the extra dependency. Reach for Microsoft.Extensions.Http.Resilience when you also want circuit-breaking, timeouts, hedging, rate limiting, or fine-grained control over which responses are retried.

Always place the cache outside the resilience pipeline:

  • Cache hits short-circuit immediately and never consume retry attempts, the timeout budget, or circuit-breaker throughput
  • The circuit breaker only observes real upstream failures — cache hits would otherwise look like an artificially healthy server and mask outages
  • Retries and timeouts only wrap the actual network call (or conditional GET during revalidation)

So the handler chain is: ReplicantHandler → ResilienceHandler → primary HTTP handler.

With HttpClientFactory

IHttpClientBuilder invokes message handlers in registration order, so the first registered handler is the outermost. Register AddReplicantCaching first, then the resilience handler:

…

snippet source | anchor

A shared singleton ReplicantCache matters here because HttpClientFactory recycles handler instances periodically — without it, a fresh CacheStore would be created against the same directory each rotation and throw.

Manual construction

For code that builds an HttpClient by hand, wrap a resilience pipeline in ResilienceHandler (from Microsoft.Extensions.Http.Resilience) and pass it as the inner handler to ReplicantHandler:

…

snippet source | anchor

Notes

  • staleIfError: true pairs naturally with the circuit breaker — when the breaker is open and BrokenCircuitException bubbles up, Replicant can serve the last successful response rather than failing outright.
  • ReplicantHandler has its own maxRetries parameter. Leave it at the default of 0 when retries are delegated to the resilience pipeline, to avoid double-retry.
  • Only GET and HEAD go through the cache path; other verbs bypass ReplicantHandler and hit the resilience pipeline directly.

HybridCache support

Replicant can serve as a disk-based L2 cache for HybridCache. Register ReplicantDistributedCache as the IDistributedCache backend, and HybridCache will automatically use it for its L2 layer (with in-memory L1 handled by HybridCache itself):

cs
var services = new ServiceCollection();
services.AddReplicantDistributedCache(cacheDirectory);
services.AddHybridCache();

snippet source | anchor

Single cache per directory

Only one cache instance (HttpCache, ReplicantCache, or ReplicantHandler with its own directory) can exist per cache directory at any time. Creating a second instance for the same directory will throw an InvalidOperationException. This prevents multiple purge timers from running against the same files.

To share a cache across multiple handlers or consumers, use a single ReplicantCache instance (see above).

Get a string

cs
var content = await httpCache.StringAsync("https://httpbin.org/json");

snippet source | anchor

cs
var lines = new List();
await foreach (var line in httpCache.LinesAsync("https://httpbin.org/json"))
{
    lines.Add(line);
}

snippet source | anchor

Get bytes

cs
var bytes = await httpCache.BytesAsync("https://httpbin.org/json");

snippet source | anchor

Get a stream

cs
using var stream = await httpCache.StreamAsync("https://httpbin.org/json");

snippet source | anchor

Download to a file

cs
await httpCache.ToFileAsync("https://httpbin.org/json", targetFile);

snippet source | anchor

Download to a stream

cs
await httpCache.ToStreamAsync("https://httpbin.org/json", targetStream);

snippet source | anchor

Manually add an item to the cache

cs
using var response = new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new StringContent("the content")
};
await httpCache.AddItemAsync(uri, response);

snippet source | anchor

Use stale item on error

If an error occurs when re-validating a potentially stale item, then the cached item can be used as a fallback.

cs
var content = httpCache.StringAsync(uri, staleIfError: true);

snippet source | anchor

Cache 404 responses

By default, 404 responses are not cached and throw an exception. Set cache404: true on the constructor to cache 404 Not Found responses to disk, avoiding repeated requests for resources known to be missing. The original 404 status code is preserved in the cached metadata.

cs
await using var cache = new HttpCache(cacheDirectory, cache404: true);
var content = await cache.StringAsync(uri);

snippet source | anchor

Retry on transient failure

Transient HTTP failures and network exceptions are automatically retried with exponential backoff when maxRetries is set. This works with both HttpCache and ReplicantHandler.

Retried status codes:

  • 408 Request Timeout
  • 500 Internal Server Error
  • 502 Bad Gateway
  • 503 Service Unavailable
  • 504 Gateway Timeout
cs
using var httpCache = new HttpCache(cacheDirectory, maxRetries: 3);
var content = await httpCache.StringAsync("https://example.com");

snippet source | anchor

cs
var handler = new ReplicantHandler(cacheDirectory, maxRetries: 3)
{
    InnerHandler = new HttpClientHandler()
};
using var client = new HttpClient(handler);
var response = await client.GetAsync("https://example.com");

snippet source | anchor

Retries use exponential backoff (200ms, 400ms, 800ms, ...). When combined with staleIfError, retries are attempted first; if all retries are exhausted, stale cached content is returned as a fallback.

Minimum freshness

By default, cached entries are revalidated when the server-provided expiry (from Expires or Cache-Control: max-age) has passed. When no expiry header is present, cached entries are served indefinitely without revalidation.

For servers that set short expiry times on content that rarely or never changes (e.g. symbol servers, package registries), set minFreshness to override the server's expiry and keep cached entries fresh for a minimum duration. This avoids unnecessary conditional GET round-trips for immutable content. This is a client-side form of heuristic freshness — useful when the server doesn't send Cache-Control: immutable or long-lived expiry headers.

cs
await using var cache = new HttpCache(
    cacheDirectory,
    minFreshness: TimeSpan.FromHours(1));
var content = await cache.StringAsync("https://httpbin.org/json");

snippet source | anchor

Customizing HttpRequestMessage

The HttpRequestMessage used can be customized using a callback.

cs
var content = await httpCache.StringAsync(
    uri,
    modifyRequest: message =>
    {
        message.Headers.Add("Key1", "Value1");
        message.Headers.Add("Key2", "Value2");
    });

snippet source | anchor

Full HttpResponseMessage

An instance of the HttpResponseMessage can be created from a cached item:

cs
using var response = await httpCache.ResponseAsync("https://httpbin.org/status/200");

snippet source | anchor

Architecture

…

HttpCache is the standalone API that owns an HttpClient. ReplicantHandler is a DelegatingHandler that plugs into an existing HttpClient pipeline. Both delegate to CacheSession which orchestrates the cache protocol using CacheStore for disk I/O. When multiple handlers need to share a cache, a single ReplicantCache is registered as a singleton. ReplicantDistributedCache implements IDistributedCache for use as a disk-based L2 backend with HybridCache.

Cache dec

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#cachehttpcachehttpclient

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具