This site provides a machine-readable index at /llms.txt.

Skip to main content Skip to navigation

AsyncLazy Pennington.Infrastructure

Thread-safe async lazy initialization with retry on failure.

Properties

Value System.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

factory Func<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;
        }
    }
}
}