TimeSpan? AbsoluteExpiration { get; init; }
Source
Gets the absolute expiration duration. The entry is evicted after this duration regardless of access patterns.
Describes the expiration behavior for a cached entry. Use the static factory methods to create common configurations.
CachePolicy Absolute(TimeSpan duration)
Creates a policy that evicts the entry after a fixed duration from creation.
duration
A CachePolicy
with absolute expiration.
ArgumentOutOfRangeException
duration
is not positive.CachePolicy AbsoluteWithStaleWhileRevalidate(TimeSpan freshDuration, TimeSpan staleDuration)
Creates an absolute-expiration policy that can serve stale values while one background refresh runs.
freshDuration
staleDuration
A CachePolicy
with absolute freshness and stale-while-revalidate behavior.
ArgumentOutOfRangeException
freshDuration
or staleDuration
is not positive.
Use this factory when a caller wants a single declaration for freshDuration
absolute freshness followed by a bounded staleDuration
revalidation window; use Absolute(TimeSpan)
followed by WithStaleWhileRevalidate(TimeSpan)
when composing or modifying an existing absolute policy. During the stale window, callers can receive out-of-date data while one background refresh per key runs; refresh failures keep the stale entry available until a later access retries. The stale-while-revalidate pattern is incompatible with sliding expiration policies.
CachePolicy WithStaleWhileRevalidate(TimeSpan duration)
Adds a bounded stale-while-revalidate window to an absolute-expiration policy.
duration
A new policy with the same absolute freshness and the requested stale window.
ArgumentOutOfRangeException
duration
is not positive.InvalidOperationException
Stale-while-revalidate is supported only for absolute-expiration policies because sliding freshness changes on access and cannot define a stable revalidation moment.
CachePolicy Sliding(TimeSpan window)
Creates a policy that evicts the entry if it is not accessed within the given window. Each access resets the sliding timer.
window
A CachePolicy
with sliding expiration.
ArgumentOutOfRangeException
window
is not positive.CachePolicy SlidingWithAbsolute(TimeSpan window, TimeSpan max)
Creates a policy that slides the entry on each access but enforces an absolute ceiling. The entry is evicted when it has been idle for window
, or unconditionally after max
from creation, whichever comes first.
window
max
A CachePolicy
with both sliding and absolute expiration.
ArgumentOutOfRangeException
window
or max
is not positive.ArgumentException
window
is greater than or equal to max
.TimeSpan? AbsoluteExpiration { get; init; }
Source
Gets the absolute expiration duration. The entry is evicted after this duration regardless of access patterns.
TimeSpan? SlidingExpiration { get; init; }
Source
Gets the sliding expiration window. The entry is evicted if not accessed within this duration. Each access resets the timer.
TimeSpan? StaleWhileRevalidate { get; init; }
Source
Gets the bounded stale window used after AbsoluteExpiration
elapses. During this window IMemo
returns the stale value immediately and refreshes it in the background.
Memoizes async factory calls using IMemoryCache
with automatic key generation and per-key thundering-herd protection.
Task<TResult> GetOrCreateCoreAsync<TState, TResult>(object key, TState state, Func<TState, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken)
Core implementation that handles cache lookup, thundering-herd synchronization, exception caching, and entry creation with the configured policy. Uses a state-passing pattern to avoid closure allocations on the hot path. key
can be a ValueTuple
for efficiency.
null
results from the factory are treated as valid cached values for reference types.
Task DrainBackgroundRefreshesAsync(CancellationToken cancellationToken = default)
Joins a snapshot of background refreshes already observed by completed stale reads.
cancellationToken
A task completing after every refresh in the snapshot has reached its original terminal state.
ObjectDisposedException
OperationCanceledException
A joined refresh may have failed; stale-refresh errors remain observed, and expired callers still receive their own factory failures. Stop new reads and await their accepted calls before using this method for shutdown, then dispose the memo/cache. Refreshes registered later are not in this snapshot. This method creates no timeout, changes no cache window and never supplies a successful cached value.
MemoryCacheEntryOptions CreateEntryOptions(SemaphoreSlim keyLock)
Creates MemoryCacheEntryOptions
with a post-eviction callback that removes the corresponding SemaphoreSlim
from _locks
to prevent unbounded growth.
keyLock
void OnCacheEntryEvicted(object key, object? value, EvictionReason reason, object? state)
Post-eviction callback that cleans up the per-key semaphore from the locks dictionary. Only removes the exact semaphore instance that was captured when the cache entry was created, preserving any newer semaphore created by concurrent callers via GetOrAdd
.
void Dispose()
Disposes the underlying locks.
int ActiveLockCount { get; }
Source
Gets the number of per-key semaphores currently retained for in-flight or failure-cached work.
Sentinel wrapper stored in the cache when a factory invocation fails, allowing subsequent waiters to short-circuit instead of re-invoking the factory.
Mutable wrapper used for entries that can serve stale values while a background refresh runs.
Provides memoized, cache-backed access to expensive computations with automatic key generation and thundering-herd protection.
Task<TResult> GetAsync<TResult>(Func<Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path and line number.
TResult
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TResult>(Func<CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path and line number. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TResult
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg, TResult>(TArg arg, Func<TArg, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and arg
.
TArg
TResult
arg
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg, TResult>(TArg arg, Func<TArg, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and arg
. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg
TResult
arg
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TResult>(TArg1 arg1, TArg2 arg2, Func<TArg1, TArg2, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments.
TArg1
TArg2
TResult
arg1
arg2
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TResult>(TArg1 arg1, TArg2 arg2, Func<TArg1, TArg2, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg1
TArg2
TResult
arg1
arg2
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, Func<TArg1, TArg2, TArg3, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments.
TArg1
TArg2
TArg3
TResult
arg1
arg2
arg3
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, Func<TArg1, TArg2, TArg3, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg1
TArg2
TArg3
TResult
arg1
arg2
arg3
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, Func<TArg1, TArg2, TArg3, TArg4, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments.
TArg1
TArg2
TArg3
TArg4
TResult
arg1
arg2
arg3
arg4
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, Func<TArg1, TArg2, TArg3, TArg4, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg1
TArg2
TArg3
TArg4
TResult
arg1
arg2
arg3
arg4
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, Func<TArg1, TArg2, TArg3, TArg4, TArg5, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments.
TArg1
TArg2
TArg3
TArg4
TArg5
TResult
arg1
arg2
arg3
arg4
arg5
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, Func<TArg1, TArg2, TArg3, TArg4, TArg5, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg1
TArg2
TArg3
TArg4
TArg5
TResult
arg1
arg2
arg3
arg4
arg5
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, Func<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments.
TArg1
TArg2
TArg3
TArg4
TArg5
TArg6
TResult
arg1
arg2
arg3
arg4
arg5
arg6
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, Func<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg1
TArg2
TArg3
TArg4
TArg5
TArg6
TResult
arg1
arg2
arg3
arg4
arg5
arg6
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, Func<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments.
TArg1
TArg2
TArg3
TArg4
TArg5
TArg6
TArg7
TResult
arg1
arg2
arg3
arg4
arg5
arg6
arg7
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, Func<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg1
TArg2
TArg3
TArg4
TArg5
TArg6
TArg7
TResult
arg1
arg2
arg3
arg4
arg5
arg6
arg7
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, Func<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments.
TArg1
TArg2
TArg3
TArg4
TArg5
TArg6
TArg7
TArg8
TResult
arg1
arg2
arg3
arg4
arg5
arg6
arg7
arg8
factory
policy
cancellationToken
The cached or freshly computed value.
Task<TResult> GetAsync<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, TResult>(TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5, TArg6 arg6, TArg7 arg7, TArg8 arg8, Func<TArg1, TArg2, TArg3, TArg4, TArg5, TArg6, TArg7, TArg8, CancellationToken, Task<TResult>> factory, CachePolicy policy, CancellationToken cancellationToken = default)
Returns a cached result or invokes factory
to compute and cache it. The cache key is automatically derived from the caller file path, line number, and the provided arguments. The CancellationToken
is forwarded to the factory for cooperative cancellation.
TArg1
TArg2
TArg3
TArg4
TArg5
TArg6
TArg7
TArg8
TResult
arg1
arg2
arg3
arg4
arg5
arg6
arg7
arg8
factory
CancellationToken
.policy
cancellationToken
The cached or freshly computed value.
A AppSurface module that registers the IMemo
caching services.