Using Windows Azure AppFabric Caching

Microsoft has announced the production version of Windows Azure AppFabric Caching service. Today, scalability and performance are the most challenging areas in enterprise application development and a distributed caching solution can avoid unnecessary round-trips to the database and give you better performance and even better availability. The Windows Azure AppFabric Caching service lets you create high performance apps for the Windows Azure cloud computing platform. Windows Azure AppFabric Caching is a distributed in-memory Cache platform hosted in Windows Azure. AppFabric Caching caches all types of data such as CLR objects, XML, binary data etc. Windows Azure AppFabric Caching service is an extensible caching platform that can also use as a storage mechanism for ASP.NET Session and ASP.NET page output caching.

Creating a cache service namespace


We need to create a Cache host on Windows Azure to working with AppFabric Caching. For this, we need to create a cache service namespace.  The following steps will create a cache service namespace


In the Windows Azure Developer portal, select the Service Bus, Access Control and Caching from the left side and this will show you AppFabric button on the top left. Expand the AppFabric button and select the Cache.

cachebutton

Click the Create Namespace button from the left corner and this will show the option to create a new cache service namespace.

namespace

After creating the Service Namespace, the Cache service  will go to the Activating state. When the service is getting ready, it will show the Active state. After the service getting the Active state, click the View Client Configuration.

configuration

The View Client Configuration will show the configuration information as shown in the below screen shot.

clientconfig

We need to copy the above configuration and put to config file of our client applications.

Creating a Cache Client


In the previous steps, we have created a Cache host on Windows Azure. To working with client, we need to add the client configuration to you App.config or Web.config file. We will get the client configuration from the View Client Configuration option from Windows Azure portal. We have to add the reference to the following assemblies from the Windows Azure AppFabric SDK v2.0

  • Microsoft.ApplicationServer.Caching.Core.dll
  • Microsoft.ApplicationServer.Caching.Client.dll


You can download the Windows Azure AppFabric SDK v2.0 from here


The following code block will create an instance of a custom class Product and will put data to AppFabric Cache.

  1. // DataCacheFactory will use configuation settings from web.config
  2. using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
  3. {                
  4.     DataCache dataCache = dataCacheFactory.GetDefaultCache();
  5.  
  6.     Product product = new Product { ProductId = 101, ProductName = "Test" };
  7.     // Put that object into the cache
  8.     dataCache.Put(product.ProductId.ToString(), product);
  9.                
  10. }

The above code will create an instance of DataCache object using the default cache configuration information from config file. The DataCache class provides methods such as  Add, Put, Get and it is very similar to existing caching APIs. The following code block will get the cached object back from the AppFabric Cache

  1. using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
  2. {
  3.     DataCache dataCache = dataCacheFactory.GetDefaultCache();
  4.     // Get the cached object back from the cache
  5.     Product cachedPerson = (Product)dataCache.Get("101");
  6.                
  7. }

 

ASP.NET providers for session state and page output caching

Windows Azure AppFabric Caching service is a highly extensible caching platform that can also use as a storage mechanism for ASP.NET Session and ASP.NET page output caching. This is very useful when we are deploying our apps onto cluster of servers. The following configuration specify that we are using  AppFabric Caching as the storage mechanism for Session state. Please keep in mind that you need to copy the client configuration to web.config file for AppFabric cache.

 

  1. <sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider" compressionEnabled="false">
  2.       <providers>
  3.         <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" useBlobMode="true" dataCacheClientName="default"/>
  4.       </providers>
  5.     </sessionState>

 

The following configuration specify that we are using AppFabric Caching as the storage mechanism for Output caching.

 

  1. <caching>
  2.       <outputCache defaultProvider="DistributedCache">
  3.         <providers>
  4.           <add name="DistributedCache"
  5.                type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache"
  6.                cacheName="default"
  7.                dataCacheClientName="default" />
  8.         </providers>
  9.       </outputCache>
  10.     </caching>

 

Windows Azure AppFabric Caching Cost

The prices of the different cache sizes are the following:


• 128 MB cache for $45.00/month
• 256 MB cache for $55.00/month
• 512 MB cache for $75.00/month
• 1 GB cache for $110.00month
• 2 GB cache for $180.00/month
• 4 GB cache for $325.00/month

Summary

Windows Azure AppFabric Caching service is a highly scalable distributed in-memory Cache platform hosted in Windows Azure that lets you build scalable and performance-driven enterprise apps for Windows Azure cloud computing platform. Azure AppFabric Caching service is can also use storage mechanism for ASP.NET Session and ASP.NET page output caching.

5 Comments

  • There is an issue when using the the V2.0 so I have to use v1.0 (ErrorCode:SubStatus:Check the client version. It should be within the allowed version range on the server. If necessary, upgrade the client to the allowed version).

    Also when i include the SSL Endpoint, this would result to Unrecognized attribute 'sslEnabled'. Do you know what is causing it?

  • Hi Arvin. The question is, why release server V2.0 when there is no working client? If Microsoft has tested V2.0 successfully (required for release) they must have a working client somewhere. I can't find any useful information on this issue.

  • Thanks Shiju, live saving blog :) Mixup's with the SDK versions has caused huge frustration, if it wasn't for this page I would have aborted my Azure AppFabric Cache efforts.

  • thanks for sharing your thoughts. the if the performance of the app is not up to the mak then it is the end user who will be suffered so the use of a distributed cache can help the cause. here is another good read about a distributed cache provider,NCache http://www.alachisoft.com/ncache/index.html

  • Ncache is extremely overpriced, not worth it IMHO.

Comments have been disabled for this content.