Skip to main content

cache.cache_factory

CacheFactory

class CacheFactory()

cache_factory

@staticmethod
def cache_factory(
seed: Union[str, int],
redis_url: Optional[str] = None,
cache_path_root: str = ".cache",
cosmosdb_config: Optional[Dict[str, Any]] = None) -> AbstractCache

Factory function for creating cache instances.

This function decides whether to create a RedisCache, DiskCache, or CosmosDBCache instance based on the provided parameters. If RedisCache is available and a redis_url is provided, a RedisCache instance is created. If connection_string, database_id, and container_id are provided, a CosmosDBCache is created. Otherwise, a DiskCache instance is used.

Arguments:

  • seed Union[str, int] - Used as a seed or namespace for the cache.
  • redis_url Optional[str] - URL for the Redis server.
  • cache_path_root str - Root path for the disk cache.
  • cosmosdb_config Optional[Dict[str, str]] - Dictionary containing 'connection_string', 'database_id', and 'container_id' for Cosmos DB cache.

Returns:

An instance of RedisCache, DiskCache, or CosmosDBCache.

Examples:

Creating a Redis cache

redis_cache = cache_factory("myseed", "redis://localhost:6379/0")

Creating a Disk cache

disk_cache = cache_factory("myseed", None)

Creating a Cosmos DB cache:

cosmos_cache = cache_factory("myseed", cosmosdb_config={
"connection_string": "your_connection_string",
"database_id": "your_database_id",
"container_id": "your_container_id"}
)