Skip to main content

RecursionSafeCache

This class provides a thread-safe caching mechanism designed to prevent infinite recursion during value initialization. It utilizes an atomic flag to track entry into the initialization logic and a synchronized storage cell to ensure the cached value is only computed once.

Attributes

AttributeTypeDescription
cacheOnceLock< T >Thread-safe storage container that holds the cached value once it has been initialized.
in_recursionAtomicBoolAtomic flag used to track whether the current thread is already performing a calculation to prevent infinite recursion.

Constructor

Signature

def RecursionSafeCache() - > [RecursionSafeCache](recursionsafecache.md?sid=definitions_recursionsafecache)

Signature

@staticmethod
def new() - > RecursionSafeCache< T >

Methods


get_or_init()

def get_or_init(
f: F: FnOnce()- > T
) - > &T

Retrieves the cached value or initializes it using the provided closure if it has not been set.

Parameters

NameTypeDescription
fF: FnOnce()- > TThe initialization closure used to produce the value if the cache is empty. Note that if this is called recursively, it may trigger safety logic.

Returns

TypeDescription
&TA reference to the cached value.