nemoguardrails.llm.cache.lfu
Least Frequently Used (LFU) cache implementation.
Module Contents
Classes
Data
API
Doubly linked list to maintain nodes with the same frequency.
Add node to the end of the list (before tail).
Remove and return a node. If no node specified, removes the first node.
Bases: CacheInterface
Least Frequently Used (LFU) Cache implementation.
When the cache reaches maxsize, it evicts the least frequently used item. If there are ties in frequency, it evicts the least recently used among them.
Get the maximum size of the cache.
Check if enough time has passed and log stats if needed.
Evict the least frequently used item from the cache.
Log current cache statistics.
Update the frequency of a node and move it to the appropriate frequency list.
Clear all items from the cache.
Check if a key exists in the cache without updating its frequency.
This is more efficient than the default implementation which uses get() and has the side effect of updating frequency counts.
Parameters:
The key to check
Returns: bool
True if the key exists in the cache, False otherwise
Get an item from the cache.
Parameters:
The key to look up
Value to return if key is not found
Returns: Any
The value associated with the key, or default if not found
Atomically get a value from the cache or compute it if not present.
This method ensures that the compute function is called at most once even in the presence of concurrent requests for the same key.
Parameters:
The key to look up
Async function to compute the value if key is not found
Value to return if compute_fn raises an exception
Returns: Any
The cached value or the computed value
Get cache statistics.
Returns: dict
Dictionary with cache statistics (if tracking is enabled)
Check if the cache is empty.
Force immediate logging of cache statistics.
Put an item into the cache.
Parameters:
The key to store
The value to associate with the key
Reset cache statistics.
Return the current size of the cache.
Check if this cache instance supports stats logging.
Node for the LFU cache doubly linked list.