Skip to content

Global Configuration

SWR .NET uses SwrOptions to control caching behavior globally. Set defaults once at startup, and every Use call inherits them — no need to pass options everywhere.

Pass a configuration callback to AddSwrForBlazor or AddSwrForAspNetCore:

Program.cs
builder.Services.AddSwrForBlazor(options =>
{
options.StaleTime = TimeSpan.FromSeconds(30);
options.CacheTime = TimeSpan.FromMinutes(5);
options.RetryCount = 2;
options.RetryBaseDelay = TimeSpan.FromSeconds(1);
options.DeduplicateRequests = true;
});
OptionDefaultDescription
StaleTime2 minutesHow long cached data is considered fresh. After this, data is returned immediately but a background revalidation is triggered.
CacheTime10 minutesHow long cached data is kept before being evicted entirely. After this, the next request is a full cache miss.
DeduplicateRequeststrueWhen multiple components request the same key concurrently, only one network request is made.
RetryCount3Number of retry attempts when a fetch fails. Uses exponential backoff.
RetryBaseDelay500msBase delay between retries. Doubles with each attempt (500ms, 1s, 2s, …).
0 StaleTime CacheTime
|--- FRESH ---|--- STALE ---|--- EXPIRED (evicted)
return return full network
cached cached + fetch required
data revalidate
  • Fresh (age < StaleTime): Cached data returned, no network request.
  • Stale (age between StaleTime and CacheTime): Cached data returned instantly, background revalidation fetches fresh data.
  • Expired (age > CacheTime): Entry evicted, next request is a cache miss.

Any Use call can override global options for that specific request:

// This request uses a shorter stale time than the global default
var result = Swr.Use("/api/live-scores", FetchScores,
options: new SwrOptions { StaleTime = TimeSpan.FromSeconds(5) });

Per-request options replace the global options entirely for that call — they don’t merge.

The options themselves are identical. The difference is service lifetime:

MethodISwr LifetimeUse Case
AddSwrForBlazorScopedEach Blazor circuit gets its own cache
AddSwrForAspNetCoreSingletonCache shared across all HTTP requests

See the Blazor Integration and ASP.NET Core Integration guides for setup details.