RazorDocs Search

Caching

Type

RunnableCachingModule

A Runnable module that registers the IMemo caching services.

Type

IMemo

Provides memoized, cache-backed access to expensive computations with automatic key generation and thundering-herd protection.

Method

GetAsync

18 overloads
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.

Type Parameters

  • TResultThe type of the cached value.

Parameters

  • factoryThe async factory to compute the value on a cache miss.
  • policyThe cache expiration policy.
  • cancellationTokenA token to cancel the cache lock acquisition.

Returns

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.

Type Parameters

  • TResultThe type of the cached value.

Parameters

  • 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.

Returns

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.

Type Parameters

  • TArgThe type of the argument.
  • TResultThe type of the cached value.

Parameters

  • 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.

Returns

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.

Type Parameters

  • TArgThe type of the argument.
  • TResultThe type of the cached value.

Parameters

  • 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.

Returns

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.

Type Parameters

  • TArg1The type of the first argument.
  • TArg2The type of the second argument.
  • TResultThe type of the cached value.

Parameters

  • 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.

Returns

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.

Type Parameters

  • TArg1The type of the first argument.
  • TArg2The type of the second argument.
  • TResultThe type of the cached value.

Parameters

  • 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.

Returns

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.

Type Parameters

  • TArg1The type of the first argument.
  • TArg2The type of the second argument.
  • TArg3The type of the third argument.
  • TResultThe type of the cached value.

Parameters

  • 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.

Returns

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.

Type Parameters

  • TArg1The type of the first argument.
  • TArg2The type of the second argument.
  • TArg3The type of the third argument.
  • TResultThe type of the cached value.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

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.

Type Parameters

  • 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.

Parameters

  • 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.

Returns

The cached or freshly computed value.

Type

CachePolicy

Describes the expiration behavior for a cached entry. Use the static factory methods to create common configurations.

Method

Absolute

CachePolicy Absolute(TimeSpan duration)

Creates a policy that evicts the entry after a fixed duration from creation.

Parameters

  • durationThe time after which the entry expires.

Returns

A CachePolicy with absolute expiration.

Exceptions

  • ArgumentOutOfRangeExceptionThrown when duration is not positive.
Method

Sliding

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.

Parameters

  • windowThe idle duration after which the entry expires.

Returns

A CachePolicy with sliding expiration.

Exceptions

  • ArgumentOutOfRangeExceptionThrown when window is not positive.
Method

SlidingWithAbsolute

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.

Parameters

  • windowThe sliding idle window.
  • maxThe absolute maximum lifetime.

Returns

A CachePolicy with both sliding and absolute expiration.

Exceptions

  • ArgumentOutOfRangeExceptionThrown when window or max is not positive.
  • ArgumentExceptionThrown when window is greater than or equal to max.
Property

AbsoluteExpiration

TimeSpan? AbsoluteExpiration { get; init; }

Gets the absolute expiration duration. The entry is evicted after this duration regardless of access patterns.

Property

SlidingExpiration

TimeSpan? SlidingExpiration { get; init; }

Gets the sliding expiration window. The entry is evicted if not accessed within this duration. Each access resets the timer.

Type

Memo

Memoizes async factory calls using IMemoryCache with automatic key generation and per-key thundering-herd protection.

Method

GetOrCreateCoreAsync

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.

Remarks

null results from the factory are treated as valid cached values for reference types.

Method

CreateEntryOptions

MemoryCacheEntryOptions CreateEntryOptions(SemaphoreSlim keyLock)

Creates MemoryCacheEntryOptions with a post-eviction callback that removes the corresponding SemaphoreSlim from _locks to prevent unbounded growth.

Parameters

  • keyLockThe specific semaphore instance to capture for reference-safe eviction cleanup.
Method

OnCacheEntryEvicted

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.

Method

Dispose

void Dispose()

Disposes the underlying locks.

Type

CachedFailure

Sentinel wrapper stored in the cache when a factory invocation fails, allowing subsequent waiters to short-circuit instead of re-invoking the factory.