CosmosDBCache

CosmosDBCache(seed: str | int, cosmosdb_config: CosmosDBConfig)

Synchronous implementation of AbstractCache using Azure Cosmos DB NoSQL API.
This class provides a concrete implementation of the AbstractCache interface using Azure Cosmos DB for caching data, with synchronous operations.
Attributes:
seed (Union[str, int]): A seed or namespace used as a partition key.
client (CosmosClient): The Cosmos DB client used for caching.
container: The container instance used for caching.
Initialize the CosmosDBCache instance.

Parameters:
NameDescription
seedType: str | int
cosmosdb_configType: CosmosDBConfig

Instance Methods

close

close(self) -> None

Close the Cosmos DB client.
Perform any necessary cleanup, such as closing network connections.


create_cache

create_cache(seed: str | int, cosmosdb_config: CosmosDBConfig) -> 

Factory method to create a CosmosDBCache instance based on the provided configuration.
This method decides whether to use an existing CosmosClient or create a new one.

Parameters:
NameDescription
seedType: str | int
cosmosdb_configType: CosmosDBConfig

from_config

from_config(seed: str | int, cosmosdb_config: CosmosDBConfig) -> 
Parameters:
NameDescription
seedType: str | int
cosmosdb_configType: CosmosDBConfig

from_connection_string

from_connection_string(
    seed: str | int,
    connection_string: str,
    database_id: str,
    container_id: str
) -> 
Parameters:
NameDescription
seedType: str | int
connection_stringType: str
database_idType: str
container_idType: str

from_existing_client

from_existing_client(
    seed: str | int,
    client: CosmosClient,
    database_id: str,
    container_id: str
) -> 
Parameters:
NameDescription
seedType: str | int
clientType: CosmosClient
database_idType: str
container_idType: str

get

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

Retrieve an item from the Cosmos DB cache.

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

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

Type: Any | None

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

set

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

Set an item in the Cosmos DB 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