Azure Cache (Preview) Step by Step

 

Windows Azure Cache is a distributed, in-memory, scalable solution that enables you to build highly scalable and responsive applications by providing super-fast access to data. Cache increases performance by temporarily storing information from other backend sources. High performance is achieved by maintaining this cache in-memory in a distributed environment.

There are mainly 3 types

  1. Cache Service (Preview)
  2. In-Role Cache
  3. Shared Cache

In this post I will explain step by step on how to develop Cache Service (Preview) with in an ASP.net application.

Step 1 : Create Cache

Create a cache for Windows azure cache service.

You can find step by step on how to create here.

Step 2 : Configure Cache

1. Create a ASP.net web project

2. Include Cache Service (Preview) NuGet package in to you project.

for that

  • Right click on the project References
  • Select “Manage NuGet packages

image

  • Search “Windows Azure Cache”. Then select and install the package highlighted in the following image.

image

3. You can  see following two sections added to your Web config.

  • dataCacheClients
  • cacheDiagnostics
  1. <configSections>
  2.   <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  3.   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  4.   <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
  5.   <section name="cacheDiagnostics" type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection, Microsoft.ApplicationServer.Caching.AzureCommon" allowLocation="true" allowDefinition="Everywhere" />
  6. </configSections>

 

4. Edit the dataCacheClients section in webconfig and add you service endpoint url which you got when creating the cache in to the web configuration file.

  1. <dataCacheClients>
  2.   <dataCacheClient name="default">
  3.     <!--To use the in-role flavor of Windows Azure Cache, set identifier to be the cache cluster role name -->
  4.     <!--To use the Windows Azure Cache Service, set identifier to be the endpoint of the cache cluster -->
  5.     <autoDiscover isEnabled="true" identifier="[Cache role name or Service Endpoint]" />
  6.     <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->
  7.     <!--Use this section to specify security settings for connecting to your cache. This section is not required if your cache is hosted on a role that is a part of your cloud service. -->
  8.     <!--<securityProperties mode="Message" sslEnabled="false">
  9.       <messageSecurity authorizationInfo="[Authentication Key]" />
  10.     </securityProperties>-->
  11.   </dataCacheClient>
  12. </dataCacheClients>

 

5. Uncoment the securityProperties section under the dataCacheClients section in webconfig and add you Authentication Key which you got when creating the cache in to the web configuration file.

  1. <securityProperties mode="Message" sslEnabled="false">
  2.         <messageSecurity authorizationInfo="[Authentication Key]" />
  3.       </securityProperties>

 

now you are ready to develop cache for your application

Step 3 : Use Cache

The Cache Service (Preview) programming model is designed for the cache-aside programming pattern. If your data is not present in the cache, your application, and not the distributed cache, must reload data into the cache from the original data source.

1. Create a class which has generic methods for accessing the cache.

  1.   public staticclass AzureCache
  2.   {
  3.  
  4.       public static T Get<T>(string key)
  5.       {
  6.           DataCache defaultCache = GetDataCache();
  7.           T value = default(T);
  8.           var cacheValue = defaultCache.Get(key);
  9.  
  10.           if (cacheValue != null)
  11.           {
  12.               try
  13.               {
  14.                   value = (T)defaultCache.Get(key);
  15.               }
  16.  
  17.               catch
  18.               {
  19.                   value = default(T);
  20.               }
  21.           }
  22.           return value;
  23.  
  24.       }
  25.  
  26.       public static void Put(string key, object value)
  27.       {
  28.           DataCache defaultCache = GetDataCache();
  29.           defaultCache.Put(key, value);
  30.       }
  31.  
  32.       public static void Remove(string key)
  33.       {
  34.           DataCache defaultCache = GetDataCache();
  35.           defaultCache.Remove(key);
  36.       }
  37.  
  38.       private static DataCache GetDataCache()
  39.       {
  40.           DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration("default");
  41.           config.ChannelOpenTimeout = new TimeSpan(0, 10, 0);
  42.           config.TransportProperties.ReceiveTimeout = new TimeSpan(0, 0, 50);
  43.           DataCacheFactory cacheFactory = new DataCacheFactory(config);
  44.           DataCache defaultCache = cacheFactory.GetDefaultCache();
  45.  
  46.           return defaultCache;
  47.       }
  48.   }

2. Add Items to cache as below

  1. AzureCache.Put("test",9900);

3. Get Items from cache as below.

  1. var value = AzureCache.Get<int>("test");

Download Sample Code

Happy Coding !!!!!

Comments

Post a Comment

Popular posts from this blog

Responsive Web Design

Contract First Development in WCF 4.5

Affine Cipher in C#