How to Manage Expiration of Cloud Service Content
Updated: February 12, 2013
The value of caching objects from hosted services in the Windows Azure CDN is realized when the content is delivered from the CDN edge cache, so content requested only once during the object’s time-to-live period will not get performance improvements from edge caching. Objects that benefit the most from caching are those that are accessed frequently during their time-to-live period.
The time-to-live period specifies that the object should be cached for that amount of time in the CDN before it is refreshed from the hosted service. The CDN attempts to refresh the object from the hosted service only once the time-to-live period has elapsed.
For static content such as images and style sheets you can control the update frequency by including a web.config in the CDN folder containing the content and modifying the clientCache settings to control the Cache-Control header for your content. The web.config settings will affect everything in the folder and all subfolders, unless overridden in another subfolder further down. For example, you can set a default time-to-live at the root to have all static content cached for 3 days, but have a subfolder that has more variable content with a cache setting of 6 hours.
The following XML shows and example of setting clientCache to specify a maximum age of 3 days:
<configuration> <system.webServer> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="3.00:00:00" /> </staticContent> </system.webServer> </configuration>
Specifying UseMaxAge adds a Cache-Control: max-age=<nnn> header to the response based on the value specified in the CacheControlMaxAge attribute. The format of the timespan is for the cacheControlMaxAge attribute is <days>.<hours>:<min>:<sec>. For more information on the clientCache node, see http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache.
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 object's last-modified time, which is the last-modified time of the object at the moment at which it was cached. The longer the period during which the object's content at the origin has not been modified, the longer the CDN caches the object without checking the origin for content updates. The assumption is that an object that has not been modified for some period of time is likely to remain unmodified for a longer interval than an object which has been modified more recently.
Currently, an object'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, an object whose last-modified time was 30 minutes ago will be considered fresh for six minutes in the CDN cache. After the object has been cached for six minutes, the CDN will, upon the next request for the cached object, check the object at the origin with a conditional request to re-cache the object 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 object at the origin with a conditional request the next time the object is accessed. For example, if an object's last-modified time is 30 days ago, the CDN will mark the object 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).
For content returned from applications such as .aspx pages, you can set the CDN caching behavior programmatically by setting the HttpResponse.Cache property. For more information on the HttpResponse.Cache property, see HttpResponse.Cache Property and HttpCachePolicy Class.
If you want to programmatically cache application content, make sure that the content is marked as cacheable by setting HttpCacheability to Public. Also, ensure that a cache validator is set. The cache validator can be a Last Modified timestamp set by calling SetLastModified, or an etag value set by calling SetETag. Optionally, you can also specify a cache expiration time by calling SetExpires, or you can rely on the default cache heuristics described earlier in this document.
For example, to cache content for one hour, add the following:
// Set the caching parameters. Response.Cache.SetExpires(DateTime.Now.AddHours(1)); Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.SetLastModified(DateTime.Now);