Getting Started with Development for Azure Cache
Updated: July 17, 2010
|For guidance on choosing the right Azure Cache offering for your application, see Which Azure Cache offering is right for me?.|
The topics in this section cover general development guidance for Microsoft Azure Cache.
There are a few basic steps to begin using caching in your application.
Create a cache.
Configure the clients to the cache.
Use the caching API to use the cache.
Create a Cache
In-Role Cache allows you to host caching within your roles. One or more named caches can be created and use in your roles, and you only pay for the virtual machine instances required to meet your application and caching needs. This type of caching is enabled through the caching settings on the role properties in Visual Studio. For step-by-step instructions, see the following topics.
Configure the Clients
You must configure your application, also referred to as a cache client, to use the cache. This involves two steps:
Reference the caching assemblies in your Visual Studio 2012 project.
Use configuration file settings or code to configure access to the cache.
For more information on configuring your .NET project to use caching, see How to: Prepare Visual Studio to Use In-Role Cache (Azure Cache).
To configure access to a cache hosted on a role, see How To Guide: Azure Caching.
Use the Caching API
The final step is to use the cache in your application code. This can be done in two ways:
Use the ASP.NET Providers for Session State and Output caching.
Use the Caching API to interact with the provisioned cache.
For more information on the ASP.NET providers, see ASP.NET 4 Cache Providers for Azure Cache.
To directly access to the cache in code, use the DataCacheFactoyConfiguration, DataCacheFactory, and DataCache classes in the caching API. The following example demonstrates how to add and retrieve a string in the
default cache using the settings from the
default dataCacheClient section of the application configuration file.
// Cache client configured by settings in application configuration file. DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration("default"); DataCacheFactory cacheFactory = new DataCacheFactory(config); DataCache defaultCache = cacheFactory.GetDefaultCache(); // Put and retrieve a test object from the default cache. defaultCache.Put("testkey", "testobject"); string strObject = (string)defaultCache.Get("testkey");
Note that with role-based In-Role Cache, the programming model has been simplified. You can access the named cache and the configuration file section through overloads of the DataCache constructor. For more information, see API Improvements for Caching on Roles. The following code example is identical in function to the previous example. The DataCache constructor overloads work only with the In-Role Cache API.
// Cache client configured by settings in application configuration file. DataCache defaultCache = new DataCache("default", "default"); // Put and retrieve a test object from the default cache. defaultCache.Put("testkey", "testobject"); string strObject = (string)defaultCache.Get("testkey");
The previous example does not work in the RoleEntryPoint methods (WebRole.cs). For more information, see How to: Create a DataCache Object in RoleEntryPoint Methods (Azure Cache).
|For complete samples that demonstrate In-Role Cache hosted on roles, see In-Role Cache Samples (Azure Cache).|
In This Section