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
| Attribute | Type | Description |
|---|---|---|
| cache | OnceLock< T > | Thread-safe storage container that holds the cached value once it has been initialized. |
| in_recursion | AtomicBool | Atomic 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
| Name | Type | Description |
|---|---|---|
| f | F: FnOnce()- > T | The 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
| Type | Description |
|---|---|
&T | A reference to the cached value. |