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.
Click the Create Namespace button from the left corner and this will show the option to create a new cache service 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.
The View Client Configuration will show the configuration information as shown in the below screen shot.
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.
- // DataCacheFactory will use configuation settings from web.config
- using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
- {
- DataCache dataCache = dataCacheFactory.GetDefaultCache();
- Product product = new Product { ProductId = 101, ProductName = "Test" };
- // Put that object into the cache
- dataCache.Put(product.ProductId.ToString(), product);
- }
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
- using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
- {
- DataCache dataCache = dataCacheFactory.GetDefaultCache();
- // Get the cached object back from the cache
- Product cachedPerson = (Product)dataCache.Get("101");
- }
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.
- <sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider" compressionEnabled="false">
- <providers>
- <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" useBlobMode="true" dataCacheClientName="default"/>
- </providers>
- </sessionState>
The following configuration specify that we are using AppFabric Caching as the storage mechanism for Output caching.
- <caching>
- <outputCache defaultProvider="DistributedCache">
- <providers>
- <add name="DistributedCache"
- type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache"
- cacheName="default"
- dataCacheClientName="default" />
- </providers>
- </outputCache>
- </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.