EN
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,
2 av 3 bedömde detta vara till hjälp - Bedöm det här ämnet

How to Manage Expiration of Blob Content

Updated: March 2, 2011

The value of caching blobs in the Windows Azure CDN is realized when the content is delivered from the CDN edge cache, so content requested only once during the blob’s time-to-live period will not get performance improvements from edge caching. The blobs that benefit the most from caching are blobs that are accessed frequently during their time-to-live period.

The time-to-live period specifies that the blob should be cached for that amount of time in the CDN before it is refreshed by the Blob service. The CDN attempts to refresh the blob from the Blob service only once the time-to-live period has elapsed.

You have two options for controlling how frequently cached blob content is updated:

  1. You can explicitly set the x-ms-blob-cache-control property on a Put Blob, Put Block List, or Set Blob Properties request, or use the Windows Azure Managed Library to set the BlobProperties.CacheControl property. Setting this property sets the value of the Cache-Control header for the blob. The value of the header or property should specify the appropriate value in seconds. For example, to set the maximum caching period to one year, you can specify the request header as x-ms-blob-cache-control: public, max-age=31556926. For details on setting caching headers, see the HTTP/1.1 specification.

  2. You can rely on the default heuristics of the cache network, rather than explicitly setting a caching value. By default, the CDN determines freshness by inferring from a ratio based on the cached blob's last-modified time, which is the last-modified time of the blob at the moment at which it was cached. The longer the period during which the blob's content at the origin has not been modified, the longer the CDN caches the blob without checking the origin for content updates. The assumption is that a blob that has not been modified for some period of time is likely to remain unmodified for a longer interval than a blob which has been modified more recently.

    Currently, a blob's freshness is calculated as 20% of the interval between the present time and the last-modified time, up to a maximum interval of 72 hours. For example, a blob whose last-modified time was 30 minutes ago will be considered fresh for six minutes in the CDN cache. After the blob has been cached for six minutes, the CDN will, upon the next request for the cached blob, check the blob at the origin with a conditional request to re-cache the blob if it has been modified since it was last cached.

    If 20% of the interval between the present time and the last-modified time is greater than the maximum interval of 72 hours, then after 72 hours have elapsed, the CDN will check the blob at the origin with a conditional request the next time the blob is accessed. For example, if a blob's last-modified time is 30 days ago, the CDN will mark the blob for a freshness check upon next access after 72 hours, and not after 6 days (as a 20% interval would suggest if there were no maximum).

    Note that the default heuristics apply only in the case where the caching period has not been explicitly specified using the x-ms-blob-cache-control property or the BlobProperties.CacheControl property.

noteNote
The freshness calculation described here does not provide a guarantee as to when any given cached blob will be refreshed. The freshness calculation and maximum interval described here may be subject to future change by Windows Azure.

Any content that you wish to cache via the CDN must be stored in your Windows Azure storage account as a publicly accessible blob. For more details on the Windows Azure Blob service, see Blob Service Concepts.

There are a few different ways that you can work with content in the Blob service:

The following code example is a console application that uses the Windows Azure Managed Library to create a container, set its permissions for public access, and create a blob within the container. It also explicitly specifies a desired refresh interval by setting the Cache-Control header on the blob. This step is optional – you can also rely on the default heuristic that's described in Overview of the Windows Azure CDN.

Assuming you have enabled the CDN as shown above, the blob that is created will be cached by the CDN. Be sure to specify your account credentials using your own storage account and access key:

using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace BlobsInCDN
{
    class Program
    {
        static void Main(string[] args)
        {
            //Specify storage credentials.
            StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey("storagesample",
                "m4AHAkXjfhlt2rE2BN/hcUR4U2lkGdCmj2/1ISutZKl+OqlrZN98Mhzq/U2AHYJT992tLmrkFW+mQgw9loIVCg==");
            
            //Create a reference to your storage account, passing in your credentials.
            CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);
            
            //Create a new client object, which will provide access to Blob service resources.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            //Create a new container.
            CloudBlobContainer container = blobClient.GetContainerReference("cdncontent");
            container.CreateIfNotExist();

            //Specify that the container is publicly accessible.
            BlobContainerPermissions containerAccess = new BlobContainerPermissions();
            containerAccess.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(containerAccess);

            //Create a new blob and write some text to it.
            CloudBlob blob = blobClient.GetBlobReference("cdncontent/testblob.txt");
            blob.UploadText("This is a test blob.");

            //Set the Cache-Control header on the blob to specify your desired refresh interval.
            blob.SetCacheControl("public, max-age=31536000");
        }
    }

    public static class BlobExtensions
    {
        //A convenience method to set the Cache-Control header.
        public static void SetCacheControl(this CloudBlob blob, string value)
        {
            blob.Properties.CacheControl = value;
            blob.SetProperties();
        }
    }
}

Test that your blob is available via the CDN-specific URL. For the blob shown above, the URL would be similar to the following:

http://az1234.vo.msecnd.net/cdncontent/testblob.txt

If desired, you can use a tool like wget or Fiddler to examine the details of the request and response.

See Also

Var detta till hjälp?
(1500 tecken kvar)

Gruppinnehåll

Lägg till
© 2013 Microsoft. Med ensamrätt.
facebook page visit twitter rss feed newsletter