2012-12-02 48 views
5

连接在使用下面的代码文章How to use the Windows Azure Blob Storage Service in .NET证明如果你有这样的在BLOB接受文件和它们储存在长时间运行的服务的一个将如何上传文件缓存到Azure的Blob存储

// 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); 
} 

存储你会每次执行所有这些步骤吗?或者,您是否可能有一个课程参考了多个请求所使用的blockBlob?多少(如果有)这是可以缓存和使用多个请求? (我猜的意思是线程)

回答

3

大多数这些对象有相当轻量级的构造函数,并不保证是线程安全的(请查看MSDN文档),所以我不会太在意缓存它们。我唯一倾向于保留为静态对象的是云存储帐户。

5

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

这不像Sql连接,程序员可以找到聪明的方法来缓存连接,因为它们开启起来很昂贵 - 这是REST调用,因此每个数据更改操作都是https调用,并且之前的所有准备工作,简直就是轻量级对象操纵

+0

为什么我不同意? https://www.youtube.com/watch?v=i5j1wWY-qus –