跳到主要内容

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

用于创建缓存实例的工厂函数。

此函数根据提供的参数决定是否创建RedisCache、DiskCache或CosmosDBCache实例。如果RedisCache可用并且提供了redis_url,则创建RedisCache实例。如果提供了connection_string、database_id和container_id,则创建CosmosDBCache。否则,使用DiskCache实例。

参数:

  • seed Union[str, int] - 用作缓存的种子或命名空间。
  • redis_url Optional[str] - Redis服务器的URL。
  • cache_path_root str - 磁盘缓存的根路径。
  • cosmosdb_config Optional[Dict[str, str]] - 包含Cosmos DB缓存的'connection_string'、'database_id'和'container_id'的字典。

返回:

RedisCache、DiskCache 或 CosmosDBCache 的实例。

示例:

创建Redis缓存

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

创建磁盘缓存

disk_cache = cache_factory("myseed", None)

创建一个Cosmos DB缓存:

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