Getting Started with Development for Windows Azure Caching
The topics in this section cover general development guidance for Windows Azure Caching.
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
Caching allows you to host Caching within your Windows 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 Caching for Windows Azure.
To configure access to a cache hosted on a Azure role, see How To Guide: Windows 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 Caching Providers for Windows Azure.
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 Caching, 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 Caching 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 (Windows Azure Caching).
|For complete samples that demonstrate Caching hosted on Windows Azure roles, see Windows Azure Caching Samples.|
In This Section