TimeSpan? AbsoluteExpiration { get; init; }
Gets the absolute expiration duration. The entry is evicted after this duration regardless of access patterns.
A Runnable module that registers the IMemo caching services.
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.
TResultThe type of the cached value.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TResultThe type of the cached value.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArgThe type of the argument.TResultThe type of the cached value.argThe argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArgThe type of the argument.TResultThe type of the cached value.argThe argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArg1The type of the first argument.TArg2The type of the second argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArg1The type of the first argument.TArg2The type of the second argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TArg6The type of the sixth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.arg6The sixth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TArg6The type of the sixth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.arg6The sixth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TArg6The type of the sixth argument.TArg7The type of the seventh argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.arg6The sixth argument passed to the factory and incorporated into the cache key.arg7The seventh argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TArg6The type of the sixth argument.TArg7The type of the seventh argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.arg6The sixth argument passed to the factory and incorporated into the cache key.arg7The seventh argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TArg6The type of the sixth argument.TArg7The type of the seventh argument.TArg8The type of the eighth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.arg6The sixth argument passed to the factory and incorporated into the cache key.arg7The seventh argument passed to the factory and incorporated into the cache key.arg8The eighth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss.policyThe cache expiration policy.cancellationTokenA token to cancel the cache lock acquisition.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.
TArg1The type of the first argument.TArg2The type of the second argument.TArg3The type of the third argument.TArg4The type of the fourth argument.TArg5The type of the fifth argument.TArg6The type of the sixth argument.TArg7The type of the seventh argument.TArg8The type of the eighth argument.TResultThe type of the cached value.arg1The first argument passed to the factory and incorporated into the cache key.arg2The second argument passed to the factory and incorporated into the cache key.arg3The third argument passed to the factory and incorporated into the cache key.arg4The fourth argument passed to the factory and incorporated into the cache key.arg5The fifth argument passed to the factory and incorporated into the cache key.arg6The sixth argument passed to the factory and incorporated into the cache key.arg7The seventh argument passed to the factory and incorporated into the cache key.arg8The eighth argument passed to the factory and incorporated into the cache key.factoryThe async factory to compute the value on a cache miss. Receives a CancellationToken.policyThe cache expiration policy.cancellationTokenA token to cancel both lock acquisition and factory invocation.The cached or freshly computed value.
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.
durationThe time after which the entry expires.A CachePolicy with absolute expiration.
ArgumentOutOfRangeExceptionThrown when duration is not positive.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.
windowThe idle duration after which the entry expires.A CachePolicy with sliding expiration.
ArgumentOutOfRangeExceptionThrown when 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.
windowThe sliding idle window.maxThe absolute maximum lifetime.A CachePolicy with both sliding and absolute expiration.
ArgumentOutOfRangeExceptionThrown when window or max is not positive.ArgumentExceptionThrown when window is greater than or equal to max.TimeSpan? AbsoluteExpiration { get; init; }
Gets the absolute expiration duration. The entry is evicted after this duration regardless of access patterns.
TimeSpan? SlidingExpiration { get; init; }
Gets the sliding expiration window. The entry is evicted if not accessed within this duration. Each access resets the timer.
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.
MemoryCacheEntryOptions CreateEntryOptions(SemaphoreSlim keyLock)
Creates MemoryCacheEntryOptions with a post-eviction callback that removes the corresponding SemaphoreSlim from _locks to prevent unbounded growth.
keyLockThe specific semaphore instance to capture for reference-safe eviction cleanup.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.
Sentinel wrapper stored in the cache when a factory invocation fails, allowing subsequent waiters to short-circuit instead of re-invoking the factory.