cache.cache
Cache
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.
redis
Create a Redis cache instance.
Arguments:
cache_seed
Union[str, int], optional - A seed for the cache. Defaults to 42.redis_url
str, optional - The URL for the Redis server. Defaults to “redis://localhost:6379/0”.
Returns:
Cache
- A Cache instance configured for Redis.
disk
Create a Disk cache instance.
Arguments:
cache_seed
Union[str, int], optional - A seed for the cache. Defaults to 42.cache_path_root
str, optional - The root path for the disk cache. Defaults to “.cache”.
Returns:
Cache
- A Cache instance configured for Disk caching.
cosmos_db
Create a Cosmos DB cache instance with ‘autogen_cache’ as database ID.
Arguments:
connection_string
str, optional - Connection string to the Cosmos DB account.container_id
str, optional - The container ID for the Cosmos DB account.cache_seed
Union[str, int], optional - A seed for the cache.client
- Optional[CosmosClient]: Pass an existing Cosmos DB client.
Returns:
Cache
- A Cache instance configured for Cosmos DB.
__init__
Initialize the Cache with the given configuration.
Validates the configuration keys and creates the cache instance.
Arguments:
config
Dict[str, Any] - A dictionary containing the cache configuration.
Raises:
ValueError
- If an invalid configuration key is provided.
__enter__
Enter the runtime context related to the cache object.
Returns:
The cache instance for use within a context block.
__exit__
Exit the runtime context related to the cache object.
Cleans up the cache instance and handles any exceptions that occurred within the context.
Arguments:
exc_type
- The exception type if an exception was raised in the context.exc_value
- The exception value if an exception was raised in the context.traceback
- The traceback if an exception was raised in the context.
get
Retrieve an item from the 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. Defaults to None.
Returns:
The value associated with the key if found, else the default value.
set
Set an item in the cache.
Arguments:
key
str - The key under which the item is to be stored.value
- The value to be stored in the cache.
close
Close the cache.
Perform any necessary cleanup, such as closing connections or releasing resources.