Cache

Cache(config: dict[str, Any])

A wrapper class for managing cache configuration and instances.
This class provides a unified interface for creating and interacting with different types of cache (e.g., Redis, Disk). It abstracts the underlying cache implementation details, providing methods for cache operations.
Attributes:
config (Dict[str, Any]): A dictionary containing cache configuration.
cache: The cache instance created based on the provided configuration.
Initialize the Cache with the given configuration.
Validates the configuration keys and creates the cache instance.

Parameters:
NameDescription
configType: dict[str, Any]

Class Attributes

ALLOWED_CONFIG_KEYS



Static Methods

cosmos_db

cosmos_db(
    connection_string: Optional[str] = None,
    container_id: Optional[str] = None,
    cache_seed: Union[str, int] = 42,
    client: Optional[Any] = None
) -> Cache

Create a Cosmos DB cache instance with ‘autogen_cache’ as database ID.

Parameters:
NameDescription
connection_stringConnection string to the Cosmos DB account.

Type: Optional[str]

Default: None
container_idThe container ID for the Cosmos DB account.

Type: Optional[str]

Default: None
cache_seedA seed for the cache.

Type: Union[str, int]

Default: 42
clientOptional[CosmosClient]: Pass an existing Cosmos DB client.

Type: Optional[Any]

Default: None
Returns:
TypeDescription
CacheCache: A Cache instance configured for Cosmos DB.

disk

disk(cache_seed: Union[str, int] = 42, cache_path_root: str = '.cache') -> Cache

Create a Disk cache instance.

Parameters:
NameDescription
cache_seedA seed for the cache.

Defaults to 42.

Type: Union[str, int]

Default: 42
cache_path_rootThe root path for the disk cache.

Defaults to “.cache”.

Type: str

Default: ‘.cache’
Returns:
TypeDescription
CacheCache: A Cache instance configured for Disk caching.

redis

redis(cache_seed: Union[str, int] = 42, redis_url: str = 'redis://localhost:6379/0') -> Cache

Create a Redis cache instance.

Parameters:
NameDescription
cache_seedA seed for the cache.

Defaults to 42.

Type: Union[str, int]

Default: 42
redis_urlThe URL for the Redis server.

Defaults to “redis://localhost:6379/0”.

Type: str

Default: ‘redis
Returns:
TypeDescription
CacheCache: A Cache instance configured for Redis.

Instance Methods

close

close(self) -> None

Close the cache.
Perform any necessary cleanup, such as closing connections or releasing resources.


get

get(
    self,
    key: str,
    default: Optional[Any] = None
) -> Any | None

Retrieve an item from the cache.

Parameters:
NameDescription
keyThe key identifying the item in the cache.

Type: str
defaultThe default value to return if the key is not found.

Defaults to None.

Type: Optional[Any]

Default: None
Returns:
TypeDescription
Any | NoneThe value associated with the key if found, else the default value.

set

set(
    self,
    key: str,
    value: Any
) -> None

Set an item in the cache.

Parameters:
NameDescription
keyThe key under which the item is to be stored.

Type: str
valueThe value to be stored in the cache.

Type: Any