cache.cosmos_db_cache
CosmosDBCache
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.
__init__
Initialize the CosmosDBCache instance.
Arguments:
seed
Union[str, int] - A seed or namespace for the cache, used as a partition key.connection_string
str - The connection string for the Cosmos DB account.container_id
str - The container ID to be used for caching.client
Optional[CosmosClient] - An existing CosmosClient instance to be used for caching.
create_cache
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.
get
Retrieve an item from the Cosmos DB cache.
Arguments:
key
str - The key identifying the item in the cache.default
optional - The default value to return if the key is not found.
Returns:
The deserialized value associated with the key if found, else the default value.
set
Set an item in the Cosmos DB cache.
Arguments:
key
str - The key under which the item is to be stored.value
- The value to be stored in the cache.
Notes:
The value is serialized using pickle before being stored.
close
Close the Cosmos DB client.
Perform any necessary cleanup, such as closing network connections.
__enter__
Context management entry.
Returns:
self
- The instance itself.
__exit__
Context management exit.
Perform cleanup actions such as closing the Cosmos DB client.