Skip to content

Cache Manager

Modules concerning cache management in Gen AI applications.

CacheManager(data_store)

Bases: Component

A class for managing cache in Gen AI applications.

This class provides functionality for storing and retrieving cache.

Attributes:

Name Type Description
data_store BaseHybridCache

The cache store to use for storing and retrieving the cache.

Initializes a new instance of the CacheManager class.

Parameters:

Name Type Description Default
data_store BaseCacheDataStore

The data store to use for the cache manager.

required

retrieve(key) async

Retrieves the cache of a given key.

This method stringifies the key and then calls the retrieve method of the data store to retrieve the cache. It returns the retrieved cache and a boolean indicating if the cache was found.

Parameters:

Name Type Description Default
key str | dict[str, Any] | tuple[Any, ...]

The key of the cache to retrieve.

required

Returns:

Type Description
tuple[Any, bool]

tuple[Any, bool]: The retrieved cache and a boolean indicating if the cache was found.

store(key, value, ttl=None) async

Stores the cache of a given key.

This method stringifies the key and then calls the store method of the data store to store the cache. If the value is None, the method will skip the storage process.

Parameters:

Name Type Description Default
key str | dict[str, Any] | tuple[Any, ...]

The key of the cache to store.

required
value Any

The value of the cache to store.

required
ttl int | str | None

The time-to-live (TTL) for the cache data. Must be an integer in seconds or a string in a valid time format (e.g. "1h", "1d", "1w", "1m", "1y"). If None, the cache data will not expire.

None

Returns:

Type Description
None

None

CacheOperationType

Bases: StrEnum

The type of operation for the cache manager.

Attribute

RETRIEVE (str): The operation type for retrieving the cache. STORE (str): The operation type for storing the cache.