cache.cache
Cache
class Cache(AbstractCache)
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:
configDict[str, Any] - A dictionary containing cache configuration.cache- The cache instance created based on the provided configuration.
redis
@staticmethod
def redis(cache_seed: Union[str, int] = 42,
redis_url: str = "redis://localhost:6379/0") -> "Cache"
Create a Redis cache instance.
Arguments:
cache_seedUnion[str, int], optional - A seed for the cache. Defaults to 42.redis_urlstr, optional - The URL for the Redis server. Defaults to "redis://localhost:6379/0".
Returns:
Cache- A Cache instance configured for Redis.
disk
@staticmethod
def disk(cache_seed: Union[str, int] = 42,
cache_path_root: str = ".cache") -> "Cache"
Create a Disk cache instance.
Arguments:
cache_seedUnion[str, int], optional - A seed for the cache. Defaults to 42.cache_path_rootstr, optional - The root path for the disk cache. Defaults to ".cache".
Returns:
Cache- A Cache instance configured for Disk caching.
cosmos_db
@staticmethod
def 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.
Arguments:
connection_stringstr, optional - Connection string to the Cosmos DB account.container_idstr, optional - The container ID for the Cosmos DB account.cache_seedUnion[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__
def __init__(config: Dict[str, Any])
Initialize the Cache with the given configuration.
Validates the configuration keys and creates the cache instance.
Arguments:
configDict[str, Any] - A dictionary containing the cache configuration.
Raises:
ValueError- If an invalid configuration key is provided.
__enter__
def __enter__() -> "Cache"
Enter the runtime context related to the cache object.
Returns:
The cache instance for use within a context block.
__exit__
def __exit__(exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType]) -> None
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
def get(key: str, default: Optional[Any] = None) -> Optional[Any]
Retrieve an item from the cache.
Arguments:
keystr - The key identifying the item in the cache.defaultoptional - 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
def set(key: str, value: Any) -> None
Set an item in the cache.
Arguments:
keystr - The key under which the item is to be stored.value- The value to be stored in the cache.
close
def close() -> None
Close the cache.
Perform any necessary cleanup, such as closing connections or releasing resources.