How to Use Windows Azure Caching (Preview) for Storing Session State

The current version of the Windows Azure SDK supports a new way of hosting caching. Instead of paying for dedicated caching resources, you can now use up to a percentage of the memory in your roles. This gives you a great deal of flexibility when allocating your cache. For each role you can specify the percent of memory that can be used for Windows Azure Caching. If you need more cache, you can increase the percentage (using the current preview portal). You can also increase the size of your roles or add additional roles.

There are two main benefits to the new Windows Azure Caching (Preview). The first is that you are using memory in your roles to store the data. This should prove to be even faster than the current caching options. The second is cost. You no longer will have additional charges for caching services. The costs you will pay are proportional to the increased load you need on the cache. Many projects have the capacity in them to utilize 10, 20 or even 30 percent of their memory to the cache. The beauty of it is that even if it is allowed to be used, it only actually uses what it needs and the rest is available to your application.

Installation of Windows Azure SDK

So that is all great, but how do we get it? First you need to be on at least the June 2012 SP1 (1.7) version of the Windows Azure SDK. If you have not done so, download and install it.

Once installed, go to your Windows Azure project role, right click and select properties. Check the “Enable Caching (Preview Release)” box on the caching tab. Under the cache cluster settings you can choose how the cache is hosted. With the co-located role you are setting the maximum amount (percentage) of memory that the cache can use. For more information you can review Microsoft’s article on setting the cache size. If you select dedicated role, the entire role’s memory will be used.

 Windows Azure Role Configurations

Figure 1 Project Settings

With the role configured, next we need to update our client to use the role. In my example, I am using an ASP.NET MVC 3 web application. The simplest way to configure the client is to use the NuGet package to configure it. Right-click on the project and select “Manage NuGet Packages” from the options. Search for Windows Azure Caching Preview. After digging through the results for a while you should find it.

Windows Azure Caching Preview

Figure 2 NuGet Packages

By installing this package, references are added and your project configuration file is modified. The references added include:

  • Microsoft.ApplicationServer.Caching.Client.dll
  • Microsoft.ApplicationServer.Caching.Core.dll
  • Microsoft.WindowsFabric.Common.dll
  • Microsoft.WindowsFabric.Data.Common.dll
  • Microsoft.Web.DistributedCache.dll (ASP.NET Web roles only)

The changes to your configuration file require you to update a value, so please be sure to follow carefully. First, a section is added to your configSections area called dataCacheClients.


Figure 3 dataCacheClients Section

Second, a dataCacheClients node is added. The key here is you MUST update the identifier field. The identifier value must match the name of the role you configured for the Windows Azure Caching.

dataCacheClients Node

Figure 4 dataCacheClients Node

Finally, to use the cache to store your session state, add the following to your configuration file.

Matching dataCacheClientName

The dataCacheClientName should match the name in Figure 4. The cacheName should match the name of the cache initially created (Figure 1).

Hopefully this walkthrough has been helpful and you now have a functioning Windows Azure Caching (Preview) in your project.

Next Post