Skip to content

Method GetOrCreateAsync<T> ​

Namespace: Belay.Core
Assembly: Belay.Core.dll

GetOrCreateAsync<T>(string, Func<Task<T>>, CancellationToken) ​

Gets a cached value or creates it asynchronously using the factory function. Thread-safe implementation prevents multiple concurrent factory executions.

csharp
public static Task<T> GetOrCreateAsync<T>(string key, Func<Task<T>> factory, CancellationToken cancellationToken = default)

Parameters ​

key string

The cache key.

factory Func<Task<T>>

The async factory function to create the value if not cached.

cancellationToken CancellationToken

Cancellation token for the operation.

Returns ​

Task<T>

The cached or newly created value.

Type Parameters ​

T

The type of the cached value.

Exceptions ​

ArgumentNullException

Thrown when key or factory is null.

Released under the Apache License 2.0.