azure - 如何将 CosmosClient.CreateAndInitializeAsync() 与 CosmosClientBuilder.Build() 结合使用

标签 azure azure-cosmosdb

此 YouTube video @27:20 讨论使用路由信息填充缓存以避免冷启动期间的延迟。 您可以尝试获取您知道不存在的文档,也可以使用 CosmosClient.CreateAndInitializeAsync()

我已经设置了此代码:

private async Task<Container> CreateContainerAsync(string endpoint, string authKey)
{
    var cosmosClientBuilder = new CosmosClientBuilder(
            accountEndpoint: endpoint,
            authKeyOrResourceToken: authKey)
        .WithConnectionModeDirect(portReuseMode: PortReuseMode.PrivatePortPool, idleTcpConnectionTimeout: TimeSpan.FromHours(1))
        .WithApplicationName(UserAgentSuffix)
        .WithConsistencyLevel(ConsistencyLevel.Session)
        .WithApplicationRegion(Regions.AustraliaEast)
        .WithRequestTimeout(TimeSpan.FromSeconds(DatabaseRequestTimeoutInSeconds))
        .WithThrottlingRetryOptions(TimeSpan.FromSeconds(DatabaseMaxRetryWaitTimeInSeconds), DatabaseMaxRetryAttemptsOnThrottledRequests);

    var client = cosmosClientBuilder.Build();
    
    var databaseResponse = await CreateDatabaseIfNotExistsAsync(client).ConfigureAwait(false);
    
    var containerResponse = await CreateContainerIfNotExistsAsync(databaseResponse.Database).ConfigureAwait(false);

    return containerResponse;
}

有没有办法将CosmosClient.CreateAndInitializeAsync()与其合并来填充缓存?

如果没有,可以这样做来填充缓存吗?

public class CosmosClientWrapper
{   
    public CosmosClientWrapper(IKeyVaultFacade keyVaultFacade)
    {           
        var container = CreateContainerAsync(endpoint, authenticationKey).GetAwaiter().GetResult();

        // Get a document that doesn't exist to populate the routing info:
        container.ReadItemAsync<object>(Guid.NewGuid().ToString(), PartitionKey.None).GetAwaiter().GetResult();     
    }
}

最佳答案

CreateAndInitialize 或 BuildAndInitialize 的要点是预先建立对所需容器执行数据平面操作所需的连接(引用 https://learn.microsoft.com/azure/cosmos-db/nosql/sdk-connection-modes#routing )。

如果容器不存在,则使用 CreateAndInitialize 或 BuildAndInitialize 没有任何意义,因为没有可以预先建立/预热的连接,因为没有要连接的目标后端端点。这就是为什么需要容器/数据库信息,因为唯一的好处是预热与支持该/那些容器的后端计算机的连接。

关于azure - 如何将 CosmosClient.CreateAndInitializeAsync() 与 CosmosClientBuilder.Build() 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74474090/

相关文章:

azure - 安全访问 Microsoft Azure Blob 存储

azure - Okta 联合 Azure AD 用户登录问题

Azure Function - 以编程方式检查其完成情况

rest - 如何使用 Azure DocumentDB REST API 获取超过 100 个查询结果

Azure - 使用 Terraform 恢复 CosmosDB 帐户

c# - Azure Function 崩溃并出现异常 ""

azure - 如何从azure函数的appinsight中排除异常?

azure - 我想在cosmos db中添加索引

azure - 从 Cosmos DB 集合中批量删除项目(文档)

mongodb - 如何引用另一个集合中的数据?