AsyncLazy Pennington.Infrastructure
Thread-safe async lazy initialization with retry on failure.
Properties
ValueSystem.Threading.Tasks.Task<T>- Task that resolves to the lazily produced value; retries automatically if the previous attempt faulted.
Constructors
.ctor
#public AsyncLazy(Func<Task<T>> factory);Initializes the instance with a factory invoked on first access.
Parameters
factoryFunc<Task<T>>
Methods
Reset
#public void Reset();Discards any cached value so the next access runs the factory again.
Pennington.Infrastructure.AsyncLazy
namespace Pennington.Infrastructure;
/// Thread-safe async lazy initialization with retry on failure.
public class AsyncLazy
{
/// Initializes the instance with a factory invoked on first access.
public AsyncLazy(Func<Task<T>> factory);
/// Discards any cached value so the next access runs the factory again.
public void Reset();
/// Task that resolves to the lazily produced value; retries automatically if the previous attempt faulted.
public Task<T> Value
{
get
{
lock (_lock)
{
if (_task is { IsFaulted: true } or null)
_task = Task.Run(_factory);
return _task;
}
}
}
}