Skip to main content

cache_factory

缓存工厂

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"}
)