nemoguardrails.llm.cache
General-purpose caching utilities for NeMo Guardrails.
Submodules
Package Contents
Classes
API
Abstract base class defining the interface for cache implementations.
All cache implementations must inherit from this class and implement the required methods to ensure compatibility with the caching system.
Get the maximum size of the cache.
Remove all items from the cache.
After calling this method, the cache should be empty.
Check if a key exists in the cache.
This is an optional method that can be overridden for efficiency. The default implementation uses get() to check existence.
Parameters:
The key to check.
Returns: bool
True if the key exists in the cache, False otherwise.
Retrieve an item from the cache.
Parameters:
The key to look up in the cache.
Value to return if key is not found (default: None).
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.
This is an optional method with a default implementation. Cache implementations should override this for better thread-safety guarantees.
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.
The default implementation returns a message indicating that statistics tracking is not supported.
Returns: dict
Dictionary with cache statistics. The format and contents
Check if the cache is empty.
Returns: bool
True if the cache contains no items, False otherwise.
Force immediate logging of cache statistics.
This is an optional method that cache implementations can override if they support statistics logging. The default implementation does nothing.
Implementations that support statistics logging should output the current cache statistics to their configured logging backend.
Store an item in the cache.
If the cache is at maxsize, this method should evict an item according to the cache’s eviction policy (e.g., LFU, LRU, etc.).
Parameters:
The key to store.
The value to associate with the key.
Reset cache statistics.
This is an optional method that cache implementations can override if they support statistics tracking. The default implementation does nothing.
Get the current number of items in the cache.
Returns: int
The number of items currently stored in the cache.
Check if this cache implementation supports statistics logging.
The default implementation returns False. Cache implementations that support statistics logging should override this to return True when logging is enabled.
Returns: bool
True if the cache supports statistics logging, False otherwise.
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.