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.
Configuring at Startup
Section titled “Configuring at Startup”Pass a configuration callback to AddSwrForBlazor or AddSwrForAspNetCore:
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;});Options Reference
Section titled “Options Reference”| Option | Default | Description |
|---|---|---|
StaleTime | 2 minutes | How long cached data is considered fresh. After this, data is returned immediately but a background revalidation is triggered. |
CacheTime | 10 minutes | How long cached data is kept before being evicted entirely. After this, the next request is a full cache miss. |
DeduplicateRequests | true | When multiple components request the same key concurrently, only one network request is made. |
RetryCount | 3 | Number of retry attempts when a fetch fails. Uses exponential backoff. |
RetryBaseDelay | 500ms | Base delay between retries. Doubles with each attempt (500ms, 1s, 2s, …). |
How Stale and Cache Time Interact
Section titled “How Stale and Cache Time Interact” 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
StaleTimeandCacheTime): Cached data returned instantly, background revalidation fetches fresh data. - Expired (age >
CacheTime): Entry evicted, next request is a cache miss.
Per-Request Overrides
Section titled “Per-Request Overrides”Any Use call can override global options for that specific request:
// This request uses a shorter stale time than the global defaultvar 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.
Blazor vs ASP.NET Core
Section titled “Blazor vs ASP.NET Core”The options themselves are identical. The difference is service lifetime:
| Method | ISwr Lifetime | Use Case |
|---|---|---|
AddSwrForBlazor | Scoped | Each Blazor circuit gets its own cache |
AddSwrForAspNetCore | Singleton | Cache shared across all HTTP requests |
See the Blazor Integration and ASP.NET Core Integration guides for setup details.