Skip to content

Configuration

Everything is configured through the builder passed to AddActionCache.

builder.Services.AddActionCache(options =>
{
    options.UseMemoryCache(memory => memory.SizeLimit = 10_000);
    options.UseRedisCache("localhost:6379");

    options.UseEntryOptions(entry =>
    {
        entry.AbsoluteExpiration = TimeSpan.FromMinutes(5);
        entry.SlidingExpiration  = TimeSpan.FromMinutes(1);
    });

    options.UseOperationTimeout(TimeSpan.FromMilliseconds(250));
    options.UseDistributedSingleFlight();
});

Builder methods

MethodPurpose
UseMemoryCache(Action<MemoryCacheOptions>)Register the in-process backend.
UseRedisCache(Action<RedisCacheOptions>)Register Redis.
UseRedisCache(string)Register Redis from a configuration string.
UseSqlServerCache(Action<SqlServerCacheOptions>)Register SQL Server.
UseAzureCosmosCache(Action<AzureCosmosCacheOptions>)Register Azure Cosmos DB.
UseEntryOptions(Action<ActionCacheEntryOptions>)Default expirations and the key-index lock timeout.
FailClosed(bool = true)Propagate backend failures instead of degrading.
UseOperationTimeout(TimeSpan)Bound a single backend operation.
UseDistributedSingleFlight(Action<ActionCacheSingleFlightOptions>?)Coalesce across instances.
UseSingleFlightOptions(Action<ActionCacheSingleFlightOptions>)Tune coalescing without switching to distributed.
UsePlaintextKeys()Emit readable, reversible keys. Debugging only.
AddBackend(Action<IServiceCollection>)Register a backend of your own.
AddDistributedLocker(...)Supply a distributed lock for a custom backend.

Registration order is chain order — see Layered backends.

ActionCacheEntryOptions

Defaults for every entry, overridable per action on [ActionCache].

PropertyTypeDefaultMeaning
AbsoluteExpirationTimeSpan?nullLifetime from when the entry is written.
SlidingExpirationTimeSpan?nullIdle lifetime, reset on each read.
LockTimeoutTimeSpan10sHow long to wait for a namespace key-index lock.
LockTimeout guards the short read-modify-write on a namespace’s key index. It is not the single-flight wait — that is ActionCacheSingleFlightOptions.WaitTimeout, which is held across the origin action and sized differently on purpose.

ActionCacheSingleFlightOptions

PropertyTypeDefaultMeaning
LeaseDurationTimeSpan30sHow long the leader may hold a key’s lock before others assume it died.
WaitTimeoutTimeSpan10sHow long a caller waits before executing uncoalesced.

Validated at startup: both must be positive, and LeaseDuration must exceed WaitTimeout.

Only locks with a time-to-live enforce the lease — that is Redis. See Stampede protection.

ActionCacheResilienceOptions

Set through FailClosed() and UseOperationTimeout().

PropertyTypeDefaultMeaning
FailClosedboolfalseRethrow backend failures instead of degrading.
OperationTimeoutTimeSpan?nullAbandon a backend operation after this long.

See Resilience.

ActionCache attribute properties

PropertyTypeDefault
Namespacestringrequired
AbsoluteExpirationlong (ms)0 — none
SlidingExpirationlong (ms)0 — none
VaryByUserVaryByUserModeAuto
VaryByHeaderstring?null
VaryByQuerystring?null
VaryByClaimstring?null
SingleFlightbooltrue

[ActionCacheEviction] and [ActionCacheRefresh] take Namespace only.

ActionCacheEndpointOptions

The Minimal API equivalent, configured through WithActionCache(ns, options => ...).

PropertyTypeDefault
AbsoluteExpirationTimeSpan?null — none
SlidingExpirationTimeSpan?null — none
VaryByUserVaryByUserModeAuto
VaryByHeaderstring?null
VaryByQuerystring?null
VaryByClaimstring?null
SingleFlightbooltrue

The expirations are TimeSpan? where the attribute takes long milliseconds: an attribute argument must be a compile-time constant, and a builder argument need not be.

The delegate runs once at registration, not per request — the options describe the endpoint, so re-running caller code on every request would only make an expensive lambda an expensive endpoint.

WithActionCacheEviction and WithActionCacheRefresh take a namespace only, matching their attributes. See Attributes.

Key contributors

builder.Services.AddActionCacheKeyContributor<TenantKeyContributor>();

See Vary-by.

Diagnostics names

ConstantValue
ActionCacheDiagnostics.MeterNameActionCache
ActionCacheDiagnostics.ActivitySourceNameActionCache