DocumentDb优化未分区更改Feed的恢复

时间:2017-01-04 21:04:30

标签: azure azure-cosmosdb

我正在创建一个我希望恢复的未分区更改Feed,例如每隔X秒轮询一次新的更改。下面的检查点变量保存最后一个响应继续响应。

    private string checkpoint;

    private async Task ReadEvents()
    {
            FeedResponse<dynamic> feedResponse;
            do
            {
                feedResponse = await client.ReadDocumentFeedAsync(commitsLink, new FeedOptions
                {
                    MaxItemCount = this.subscriptionOptions.MaxItemCount,
                    RequestContinuation = checkpoint
                });

                if (feedResponse.ResponseContinuation != null)
                {
                    checkpoint = feedResponse.ResponseContinuation;
                }

               // Code to process docs goes here...

            } while (feedResponse.ResponseContinuation != null);
    }

注意使用&#34; if&#34;阻止检查站。这样做是因为如果我把它留下来,responseContinuation会被设置为null,这将基本上重新开始轮询周期,因为将请求延续设置为null将拉动更改源中的第一组文档。

但是,缺点是每个轮询循环都会重放上一组文档而不是任何其他更改。我有什么办法可以进一步优化这一点,还是改变Feed API的限制?

1 个答案:

答案 0 :(得分:1)

要阅读更改Feed,您必须使用CreateDocumentChangeFeedQuery(从不重置ResponseContinuation),而不是ReadDocumentFeed(当没有更多结果时设置为null)。

请参阅https://docs.microsoft.com/en-us/azure/documentdb/documentdb-change-feed#working-with-the-rest-api-and-sdk了解样本。