static void

MSDN How to, MSDN

NB: HttpApplicationState, HttpSessionState, HttpRuntime.Cache and .Net4 MemoryCache work in-process (memory) in Azure (asp caching). You can even use Session with SqlAzure (but why?).

NB: Windows Azure Shared Caching is different- it's the older paid-for caching.

Configuration

You need to change the web/app.config, specifically the role name here (otherwise the error is ErrorCode<UnspecifiedErrorCode>:SubStatus<ES0001>:The role [cache cluster role name] was not found in the current deployment.)

<dataCacheClients>
  <dataCacheClient name="default">
    <autoDiscover isEnabled="true" identifier="MyWebRole" />

DataCache

Named caches

Example

using System;
using Microsoft.ApplicationServer.Caching;
 
namespace MartinWeb.Cloud
{
    public class CacheCloud
    {
        private readonly string _cacheName;
 
        public CacheCloud(string cacheName = null)
        {
            _cacheName = cacheName;
        }
 
        private DataCache FindDataCache()
        {
            if (string.IsNullOrEmpty(_cacheName))
            {
                return new DataCacheFactory().GetDefaultCache();
            }
            return new DataCache(_cacheName);
        }
 
        public void Add(string key, object value)
        {
            Add(key, value, TimeSpan.FromMinutes(10));
        }
 
        public void Add(string key, object value, TimeSpan timeSpan)
        {
            var dataCache = FindDataCache();
            //for example only- why not just use Put?
            try
            {
                dataCache.Add(key, value, timeSpan);
            }
            catch (DataCacheException)
            {
                //already exists, add or replace
                dataCache.Put(key, value, timeSpan);
            }
        }
 
        public T Get<T>(string key) where T : class
        {
            var dataCache = FindDataCache();
            return dataCache.Get(key) as T;
        }
 
        public TimeSpan RemainingTimeToLive(string key)
        {
            var dataCache = FindDataCache();
            return dataCache.GetCacheItem(key).Timeout;
        }
    }
}