Getting Started with Development for Azure In-Role Cache
Important
Microsoft recommends all new developments use Azure Redis Cache. For current documentation and guidance on choosing an Azure Cache offering, see Which Azure Cache offering is right for me?
The topics in this section cover general development guidance for In-Role Cache.
Development Process
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 Azure 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 Azure In-Role Cache.
To configure access to a cache hosted on a Azure role, see How To Guide: Azure In-Role Cache.
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 In-Role Cache.
To directly access to the cache in code, use the DataCache class in the In-Role Cache 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.
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");
' Cache client configured by settings in application configuration file.
Dim defaultCache As New DataCache("default", "default")
' Put and retrieve a test object from the default cache.
defaultCache.Put("testkey", "testobject")
Dim strObject As 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 in Azure In-Role Cache.
Tip
For complete samples that demonstrate In-Role Cache hosted on Azure roles, see Azure In-Role Cache Samples.