2012-12-02 234 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 –