【问题标题】:Cache the connection to Azure Blob storage缓存与 Azure Blob 存储的连接
【发布时间】:2012-12-05 11:13:09
【问题描述】:

在文章How to use the Windows Azure Blob Storage Service in .NET 中,以下代码用于演示如何上传文件

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
}

如果您有一个长时间运行的服务,它接受文件并将它们存储在 blob 存储中,您会每次都执行所有这些步骤吗?或者您可能有一个引用了多个请求使用的blockBlob 的类?从多个请求中缓存和使用多少(如果有的话)是可以的? (我猜这意味着线程)

【问题讨论】:

    标签: azure connection azure-storage


    【解决方案1】:

    我同意@knightpfhor,没有什么可以缓存的。在您调用 UploadFromStream 之前,没有调用过长时间运行的事务。一切都在内存中,构建对象。

    这不像 Sql 连接,程序员会找到巧妙的方法来缓存连接,因为它们的打开成本很高 - 这是 REST 调用,因此每个数据更改操作都是 https 调用以及之前的所有准备工作,只是轻量级的对象操作

    【讨论】:

      【解决方案2】:

      这些对象中的大多数都有非常轻量级的构造函数,并且不能保证是线程安全的(查看 MSDN 文档),所以我不会太担心缓存它们。我倾向于将其作为静态对象保留的唯一一个是云存储帐户。

      【讨论】:

        猜你喜欢
        • 2022-08-03
        • 2021-07-11
        • 2017-09-17
        • 1970-01-01
        • 2022-12-11
        • 2020-10-08
        • 2018-07-09
        • 1970-01-01
        • 2018-03-13
        相关资源
        最近更新 更多