Tuesday, April 8, 2014

Working with Cache in Azure

Working with Caching in Azure

Windows Azure Caching

Windows Azure is providing a caching service which can be used as a caching layer for your Cloud apps hosted on Windows Azure. Windows Azure Caching service is a highly scalable, distributed, in-memory Cache which allows developers to cache all data types, thus reducing the load on databases and improving the performance of the Windows Azure applications.
Using Windows Azure Caching, you can do the following:
  • Cache data on Windows Azure Caching
  • Use as storage mechanism for ASP.NET session state and ASP.NET page output caching
The new Windows Azure Caching allows you to deploy Cache on the virtual machines in which you have hosted your Windows Azure Cloud Services. Now you can cache on Web Roles and Worker Roles that can be easily scaled, managed and monitored just like your Cloud Services. You can also use the existing Windows Azure Shared Caching along with the new Caching option on the Cloud Services.

Cache Cluster

Cache Cluster is the Cloud Service role instance that you use for hosting your Cache. You can deploy the cache cluster in two different ways. You can deploy in a dedicated VM as well as you can share Caching with your Role instances.
The following are the two types of deployment options available for configuring cache.
· Co-located Role caching - The Cache shares the Virtual Machine resources with the Cloud Service application. In this option, you can share the resources of Web Role and Worker Role for Cache.
· Dedicated Role caching - The role instances dedicated only for your caching. In this way, you would be deploy the Cache in a dedicated VM by using a Cache Worker Role.

Configuring Co-located Role Cache Cluster

If you want to configure your cache along with your Role instances, you can choose the co-located Role cache cluster as the deployment option.
The following steps will configure the Windows Azure Role for using co-located Role Caching
1. In Visual-Studio, right-click on the Windows Azure Role, and click properties.
2. In the properties window, choose the 'Caching' tab.
pic-1-co-located-role
3. In the Caching tab, check the 'Enable Caching (Preview Release)' checkbox.
4. Choose 'Co-located Role' for the Cache Cluster settings. By default, this will be 'Co-located Role' when we enable Caching.
5. Choose the percentage of Cache size. By default Cache size will be 30 percentage. This means that 30 percentage of VM memory allocated for Cache. You can change the Cache size whatever you want for your caching solution.
6. Configure the settings of Caches. We can specify the 'Time to Live (min)' for the time interval of Cache for expiry and 'Expiration Type' for specifying the expiration type of Cache object.
By default, value of “The Time to Live” would be 10 minutes and the Expiration Type would be Absolute. The Absolute expiration type is specifying that the timer for the expiration would be start after the data added into Cache object. If the expiration time is 10 minutes, the cache will be expire after 10 minutes when the item is added into Cache. The other expiration types are 'Sliding Window' and 'None'. If the Expiration Type is 'Sliding Window', the timer for the expiration time would be reset after each item is retrieved from the Cache. If expiration type is 'None', items in the cache will not be expired. In this scenario, you have to specify “Time to Live (min)” as 0.
pic-1.1-co-located-role-settings

Configuring Dedicated Role Cache Cluster

If you want to host your cache in a dedicated VM, you can choose the Dedicated Role cache cluster as the deployment option.
The following steps will configure the Dedicated Role cache cluster
1. Add Cache Worker Role project - To configure Dedicated Role cache cluster, you need to add a Cache Worker Role to your solution. Add a New Cache Worker Role project onto
your Windows Azure project.
pic-2-new-workerrole
pic-3-cacheworkerrole-template
When we are adding a new Cache Worker Role project, it will automatically enable Caching with Dedicated Role as the Cache cluster.
pic-4-workerrole-caching-properties
2. Configure the settings for Dedicated Cache Role - Right-click on the Cache Worker Role from the Windows Azure project, and click properties and choose Configuration
pic-5-dedicatedcacherole-properties
By default, the VM instance count would be 1 and Virtual Machine size would be small. You can change these setting based on your hosting plan for Dedicated Role Cache Cluster.
3. Configure the Cache settings for expiration time and expiration type.
pic-1.1-co-located-role-settings

Working with Cache Clients

In order to work with Windows Azure caching from Cloud Services applications, we need to install Windows Azure Caching client library onto your Web Role and Worker Role applications. The following steps will add a Windows Azure client library to a Web Role application. We can install the Windows Azure Caching client library by using NuGet.
From the following NuGet packages, Install Microsoft.WindowsAzure.Caching
pic-7-install-nuget-package
The Windows Azure Caching NuGet package will install the necessary assmebly reference and will also add the configuartion setting on the configuaration file. You can also manually add the necessary assmeblies from the folder C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\2012-06\ref\CachingPreview if you have installed Windows Azure SDK 1.7 on your system. At the time of this writing, Azure SDK 1.8 is the latest.
The Windows Azure Caching NuGet package will add a configuration element dataCacheClients into the web.config file.
The following configuration setting in the web.config file will configured a cache client for a web role application.
    
   


  
  
    
    
      
      
    

  

The identifier in the config settings specifies the name of the role that hosts the cache cluster

No comments:

Post a Comment