CloudBlobContainer.ListBlobs() - 使用 ToList() 降低交易成本

发布于 2024-12-08 11:19:33 字数 490 浏览 0 评论 0原文

我有两个代码示例:

var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs();
foreach (var blob in blobs)
{
    //Do somthing
}

第二

var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs().ToList();
foreach (var blob in blobs)
{
     //Do somthing
}

个示例会在“事务方面”带来任何优势吗?
每个示例中对 Blob 存储进行了多少事务?

I have two code examples :

var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs();
foreach (var blob in blobs)
{
    //Do somthing
}

and this :

var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs().ToList();
foreach (var blob in blobs)
{
     //Do somthing
}

Will the second example give any advantage "transaction-wise" ?
How many transaction are made to the blob storage in each example ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2024-12-15 11:19:34

编辑:看来这个问题是参考Martin Ingvar Kofoed Jensen 的博客文章。这个问题和那个问题之间的区别在于对 .Where(...).SingleOrDefault() 的调用。由于 LINQ 是惰性求值的,因此在 Container.ListBlobs() 阶段,它仍然是一个 IEnumerable,尚未调用 REST API(尚未检索到任何数据) )。一旦发生结果操作(例如 ToList()SingleOrDefault()),就会下载数据。由于在以下代码中循环内的惰性列表上调用了非惰性操作,因此它会为每个循环迭代产生一个事务:

foreach (string filePath in allFilesInStartFolder)
{
    string fileHash = GetFileHashFromCache(filePath, lastSync);

    /* Checking for added files */
    var blob = blobs.Where(b => b.LocalPath == filePath).SingleOrDefault();
    // ^^ This is a non-lazy op on a lazy evalution, so it causes a REST call.
    ....
}

关于实际编写的问题:两个代码片段都会产生 单个事务(最多 5,000 个 blob)。我在 LinqPad/Fiddler,我只看到一个 单个 API 调用 生成 blob 列表(用 {mystore} 替换我们的存储名称):

https://{mystore}.blob.core.windows.net/
    {mystore}?restype=container&comp=list&delimiter=%2F&timeout=90

根据 列出 Blob REST API,一次调用最多可返回 5,000 个结果。因此,如果要列出整个容器中的所有 blob,最多需要 (# blobs / 5000) 个事务。

Edit: It appears this question is in reference to a blog post by Martin Ingvar Kofoed Jensen. The difference between this question and that question is the call to .Where(...).SingleOrDefault(). Because LINQ is lazy-evaluated, at the stage of Container.ListBlobs() it is still an IEnumerable and hasn't yet called the REST API (no data has been retrieved). As soon as a result operation occurs (such as ToList() or SingleOrDefault()), the data is downloaded. Since a non-lazy operation is called on a lazy list inside the loop in the following code, it incurs a transaction for every loop iteration:

foreach (string filePath in allFilesInStartFolder)
{
    string fileHash = GetFileHashFromCache(filePath, lastSync);

    /* Checking for added files */
    var blob = blobs.Where(b => b.LocalPath == filePath).SingleOrDefault();
    // ^^ This is a non-lazy op on a lazy evalution, so it causes a REST call.
    ....
}

Regarding the question as it's actually written: Both code snippets will incur a single transaction (up to 5,000 blobs). I tested both code snippets in LinqPad/Fiddler, and I only see a single API call to generate the list of blobs (replaced our storage name with {mystore}):

https://{mystore}.blob.core.windows.net/
    {mystore}?restype=container&comp=list&delimiter=%2F&timeout=90

According to the documentation for List Blobs REST API, up to 5,000 results can be returned in a single call. So if you want to list all blobs in the entire container, it will take at most (# blobs / 5000) transactions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文