Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache the connection to Azure Blob storage

In the article How to use the Windows Azure Blob Storage Service in .NET the following code is used to demonstrate how one might upload a file

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

If you had a long running service that was accepting files and storing them in blob storage would you perform all of these steps every time? Or would you maybe have a class that had a reference to blockBlob that was used by multiple requests? How much (if any) of this is it okay to cache and use from multiple requests? (which I guess means threads)

like image 371
Aran Mulholland Avatar asked Dec 02 '12 05:12

Aran Mulholland


People also ask

What is Azure Blob Storage?

Azure Blob storage The Blob storage service in Azure Storage is one of several Azure-based origins integrated with Azure Content Delivery Network (CDN). Any publicly accessible blob content can be cached in Azure CDN until its time-to-live (TTL) elapses. The TTL is determined by the Cache-Control header in the HTTP response from the origin server.

What blob content can be cached in azure CDN?

Any publicly accessible blob content can be cached in Azure CDN until its time-to-live (TTL) elapses. The TTL is determined by the Cache-Control header in the HTTP response from the origin server.

Is there a blob caching option in the Azure portal?

Unfortunately the Azure Portal don't allow access to the caching settings of the blobs. There is no such thing in the Blob Properties shown. Luckily, you can easily run scripts in the Azure Portal. Azure Cloud Shell enables you to easily run PowerShell- or Bash-script from within the Azure Portal.

How do I set a BLOB storage service's cache-control headers?

To set a Blob storage service's Cache-Control headers by using global caching rules: Under Global caching rules, set Query string caching behavior to Ignore query strings and set Caching behavior to Override. For Cache expiration duration, enter 3600 in the Seconds box or 1 in the Hours box.


1 Answers

I concur with @knightpfhor, there is nothing to cache. Until you call UploadFromStream, no long-running transactions have been called. Everything is in memory, constructing objects.

This is not like a Sql Connection, where programmers would find clever ways to cache connections because they were expensive to open - this is REST calls, so every data-changing action is an https call and all the preparation prior to it, is simply light-weight object manipulation

like image 94
Igorek Avatar answered Oct 01 '22 07:10

Igorek