DocumentDB客户端生存期 [英] DocumentDB client lifetime

查看:125
本文介绍了DocumentDB客户端生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要访问DocumentDB/CosmosDB,我正在使用软件包Microsoft.Azure.DocumentDB.Core(v1.3.2).创建和初始化DocumentClient类时,我已经注意到:

To access DocumentDB/CosmosDB I'm using package Microsoft.Azure.DocumentDB.Core(v1.3.2). I have noticed when I create and initialise DocumentClient class:

var documentClient = new DocumentClient(new Uri(endpointUrl), primaryKey);
await documentClient.OpenAsync();

有许多向终端发出的请求,以获取有关索引的信息和其他信息.确切地说,在.OpenAsync()上有9个HTTP请求.就性能而言,这使客户端的创建和激活成为一项非常昂贵的操作-花费一秒钟的时间才能将所有请求送回家.

There is a number of requests fired to the endpoint to get information about indexes and other information. To be exact there are 9 HTTP requests going out on .OpenAsync(). This makes the creation and activation of the client a very costly operation in terms of performance - takes up to a second to get all the requests back home.

因此,为了减轻此昂贵的操作,我将DocumentClient设置为单例,并在应用程序的生命周期内始终保留引用.

So to mitigate this costly operation I'm making DocumentClient to be a singleton and keep the reference around for the lifetime of the application.

应用程序是Asp.Net Core MVC,这可能会使该对象的引用在内存中保持几天.

Application is Asp.Net Core MVC and this might keep the reference of this object in memory for days.

问题:可以将此对象长时间保持为单例吗?如果没有,应该采取什么处置策略?还是有办法降低初始化的费用(即不发出这些初始请求?).

Question: is it OK to keep this object as a singleton for that long? if not, what should be the strategy to dispose it? Or is there a way to make the initialisation cheaper (i.e. don't make these initial requests?).

推荐答案

我们也想知道这一点,并且发现了这一点:

We've wondered that for ourselves as well and found this:

来自文档

SDK使用技巧#1:在应用程序的生存期内使用Singleton DocumentDB客户端请注意,每个DocumentClient实例都是线程安全的,并且在直接模式下运行时会执行有效的连接管理和地址缓存.为了允许DocumentClient进行有效的连接管理和更好的性能,建议在应用程序的生命周期内为每个AppDomain使用一个DocumentClient实例.

SDK Usage Tip #1: Use a singleton DocumentDB client for the lifetime of your application Note that each DocumentClient instance is thread-safe and performs efficient connection management and address caching when operating in Direct Mode. To allow efficient connection management and better performance by DocumentClient, it is recommended to use a single instance of DocumentClient per AppDomain for the lifetime of the application.

我想这仍然有效,现在您也可以使用它来寻址CosmosDB.

I suppose this is still valid now you can address CosmosDB with it as well.

这篇关于DocumentDB客户端生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆