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
- Cache Service (Preview)
- In-Role Cache
- 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”
- Search “Windows Azure Cache”. Then select and install the package highlighted in the following image.
3. You can see following two sections added to your Web config.
- dataCacheClients
- cacheDiagnostics
- <configSections>
- <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
- <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
- <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
- <section name="cacheDiagnostics" type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection, Microsoft.ApplicationServer.Caching.AzureCommon" allowLocation="true" allowDefinition="Everywhere" />
- </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.
- <dataCacheClients>
- <dataCacheClient name="default">
- <!--To use the in-role flavor of Windows Azure Cache, set identifier to be the cache cluster role name -->
- <!--To use the Windows Azure Cache Service, set identifier to be the endpoint of the cache cluster -->
- <autoDiscover isEnabled="true" identifier="[Cache role name or Service Endpoint]" />
- <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->
- <!--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. -->
- <!--<securityProperties mode="Message" sslEnabled="false">
- <messageSecurity authorizationInfo="[Authentication Key]" />
- </securityProperties>-->
- </dataCacheClient>
- </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.
- <securityProperties mode="Message" sslEnabled="false">
- <messageSecurity authorizationInfo="[Authentication Key]" />
- </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.
- public staticclass AzureCache
- {
- public static T Get<T>(string key)
- {
- DataCache defaultCache = GetDataCache();
- T value = default(T);
- var cacheValue = defaultCache.Get(key);
- if (cacheValue != null)
- {
- try
- {
- value = (T)defaultCache.Get(key);
- }
- catch
- {
- value = default(T);
- }
- }
- return value;
- }
- public static void Put(string key, object value)
- {
- DataCache defaultCache = GetDataCache();
- defaultCache.Put(key, value);
- }
- public static void Remove(string key)
- {
- DataCache defaultCache = GetDataCache();
- defaultCache.Remove(key);
- }
- private static DataCache GetDataCache()
- {
- DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration("default");
- config.ChannelOpenTimeout = new TimeSpan(0, 10, 0);
- config.TransportProperties.ReceiveTimeout = new TimeSpan(0, 0, 50);
- DataCacheFactory cacheFactory = new DataCacheFactory(config);
- DataCache defaultCache = cacheFactory.GetDefaultCache();
- return defaultCache;
- }
- }
2. Add Items to cache as below
- AzureCache.Put("test",9900);
3. Get Items from cache as below.
- var value = AzureCache.Get<int>("test");
Happy Coding !!!!!
Very informative! Keep share your information like this Azure Online Training
ReplyDeleteWell said. concepts are really understandable. thanks for sharing this blog.
ReplyDeleteAzure Training in Chennai | Certification | Azure Online Training Course | Azure Training in Bangalore | Certification | Azure Online Training Course | Azure Training in Hyderabad | Certification | Azure Online Training Course | Azure Training in Pune | Certification | Azure Online Training Course | Azure Training | microsoft azure certification | Azure Online Training Course