此 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/