Develop for Azure Managed Cache Service
Updated: August 25, 2015
|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?|
Managed Cache Service provides a set of APIs to add, update, and remove items from a cache. Using the classes and methods in the Microsoft.ApplicationServer.Caching namespace is a direct way of interacting with a cache. Whereas, using one of the ASP.NET providers is an indirect way of using a cache. The topics in this section cover direct programmatic use of Managed Cache Service. For more information about the ASP.NET providers for Managed Cache Service, see ASP.NET 4 Cache Providers for Azure Managed Cache Service.
The Managed Cache Service 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.
Design application code to function independent of the cache, so that the application continues to function if the cached data is not available. Because data in the cache is not persisted in a durable fashion, the possibility exists that the data in the cache could be unavailable. For example, if a cache is scaled to a different cache offering, the cached items in memory are lost. Requesting an item that does not exist in the cache is referred to as a cache miss.
When using the Managed Cache Service high availability feature, backup copies of cached items help to guard against machine and process failures on a cache. Even with high availability enabled, there is a chance, no matter how remote, that all cache data is lost due to a major disaster event. Items that require persistence must use a database or other persistent-storage techniques. For more information, see High Availability for Azure Managed Cache Service.
There are many other reasons that your code might encounter a cache miss. For example, the cache item might have expired or been evicted based on normal expiration and eviction policies. Regardless of the reason, the application code must be able to access the original data source to repopulate the item in the cache. For more information, see Expiration and Eviction for Azure Managed Cache Service.
There are a few basic steps to begin using caching in your application.
Create a cache.
Configure the clients to use the cache.
Use the Managed Cache Service API to use the cache.
Caches in Managed Cache Service are created using Powershell cmdlets and then managed in the Azure Management Portal. For guidance on creating a cache, see How to: Create a Cache for Azure Managed Cache Service.
You must configure your application, also referred to as a cache client, to use the cache. This involves two steps:
Reference the Managed Cache Service assemblies in your Visual Studio project.
Use configuration file settings or code to configure access to the cache.
Managed Cache Service provides a caching NuGet package that adds the assembly references and inserts the required configuration into your project's web.config or app.config file. After installing the Cache NuGet Package, you edit the newly added configuration and specify the cache endpoint and access key. For step-by-step guidance, see How to: Configure a Cache Client for Azure Managed Cache Service.
The final step is to use the cache in your application code. This can be done in two ways:
Use the ASP.NET 4 Cache Providers for Azure Managed Cache Service for Session State and Output caching.
Use the Caching API to interact with the provisioned cache from within your application.
To directly access to the cache in code, use the DataCacheFactoyConfiguration, DataCacheFactory, and DataCache classes in the Managed Cache Service API. The following example demonstrates how to add and retrieve a string in the
default cache using the settings from the
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");
You can also access the named cache and the configuration file section through overloads of the DataCache constructor. The following code example is identical in function to the previous example.
// 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 method overloads. For more information, see How to: Create a DataCache Object in RoleEntryPoint Methods for Azure Managed Cache Service.|
Additional on using the Managed Cache Service API to access a cache is provided in the Using Azure Managed Cache Service section.
For complete samples that demonstrate Managed Cache Service, see Azure Managed Cache Service Samples.
ConceptsHow to: Create a Cache for Azure Managed Cache Service
Other ResourcesAzure Managed Cache Service