SALES: 1-800-867-1380

How to: Configure a Cache Client Programmatically (Azure Shared Caching)

Updated: June 19, 2014

ImportantImportant
Please note the Azure Shared Caching service will be retired on September 1, 2014 and with it the Azure Silverlight-based portal. Once the Shared Caching service is retired, all remaining Shared Cache deployments will be deleted. Microsoft strongly encourages you to migrate at the earliest opportunity all existing Shared caches to either the Managed Cache Service (currently in GA) or to the new Azure Redis Cache (currently in Preview). For migration guidance, including guidance for migrating without making code changes, see Migrate from Shared Caching. For more information about the current Azure Cache offerings, see Azure Cache.

This topic explains how to configure a cache client to use a provisioned Microsoft Azure cache. A cache client is any application that accesses the cache programmatically. The configuration can be done programmatically or through an application configuration file. This topic demonstrates how to programmatically configure the cache client. To see how to use the alternative technique of the configuration file, see How to: Configure a Cache Client using the Application Configuration File (Azure Shared Caching).

noteNote
Much of the cache client programming model for the cloud-based is shared with the on-premises Microsoft AppFabric 1.1 for Windows Server. For more information, see Cache API Support in Azure Cache.

To programmatically configure the cache client

  1. First prepare your Visual Studio 2012 project to use Microsoft Azure Cache. For more information, see How to: Prepare Visual Studio to Use In-Role Cache (Azure Cache).

  2. On the , view the settings for the target cache. For more information, see Managing Caches (Azure Shared Caching).

  3. Create an instance of the DataCacheFactoryConfiguration class.

  4. Create an array with one element of the type DataCacheServerEndpoint.

  5. In the Properties window, note the value of cache Service URL. This is the host name for your Azure cache. This is the host name for your Azure Labs cache. Construct a new DataCacheServerEndpoint object with this host name and the default port 22233. Assign this to your one-element array.

  6. Assign the DataCacheServerEndpoint array to the Servers property of the DataCacheFactoryConfiguration object.

  7. On the Properties window, also note the value of the cache Authentication Token. This is an ACS key that secures access to your cache.

  8. The ACS key must be passed to the DataCacheSecurity constructor as a System.Security.SecureString. To create the SecureString, use code similar to the following example:

    string strACSKey = "iz8zOmJ0dJBzzzzxa8JxdGJkb281bzxudGF0az9udGxzdGluZi111z8oZS5Ji28lP38jb250Pm9sLmlud" +
        "DMud2luZG93Pi1pb8Qubmx0L1dSQxB2MP45LiZxd25lPii0eDJJZQA5SlZQQUQ2xzIizk8iazlES1P08X8zOFIxZjdzZxp0i3lF" +
        "Q2FFPSZodJRzOi8xS8JxdGJEb281bzxudGF0az9uxGxzdGluZi5jiz8oZS5pb8QzL8dpbmRxd3MQaz50Lm5ldA==";
    var secureACSKey = new SecureString();
    foreach (char a in strACSKey)
    {
       secureACSKey.AppendChar(a);
    }
    secureACSKey.MakeReadOnly();
    
    TipTip
    This step requires a reference to the System.Security namespace.

  9. Create a new DataCacheSecurity object, and pass the Authentication Token as a SecureString to the constructor.

  10. Assign the DataCacheSecurity object to the SecurityProperties property of the DataCacheFactoryConfiguration object.

  11. Create a new DataCacheFactory object, and pass the DataCacheFactoryConfiguration object to the constructor.

  12. Use the DataCacheFactory object to call GetDefaultCache. This returns a DataCache object that can be used to programmatically access the cache.

The following code example shows a programmatic cache client configuration.

// Declare array for cache host.
DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
servers[0] = new DataCacheServerEndpoint("yourcachename.cache.windows.net", 22233);

// Setup DataCacheSecurity configuration.
string strACSKey = "iz8zOmJ0dJBzzzzxa8JxdGJkb281bzxudGF0az9udGxzdGluZi111z8oZS5Ji28lP38jb250Pm9sLmlud" +
    "DMud2luZG93Pi1pb8Qubmx0L1dSQxB2MP45LiZxd25lPii0eDJJZQA5SlZQQUQ2xzIizk8iazlES1P08X8zOFIxZjdzZxp0i3lF" +
    "Q2FFPSZodJRzOi8xS8JxdGJEb281bzxudGF0az9uxGxzdGluZi5jiz8oZS5pb8QzL8dpbmRxd3MQaz50Lm5ldA==";
var secureACSKey = new SecureString();
foreach (char a in strACSKey)
{
   secureACSKey.AppendChar(a);
}
secureACSKey.MakeReadOnly();
DataCacheSecurity factorySecurity = new DataCacheSecurity(secureACSKey);

// Setup the DataCacheFactory configuration.
DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();
factoryConfig.Servers = servers;
factoryConfig.SecurityProperties = factorySecurity;

// Create a configured DataCacheFactory object.
DataCacheFactory cacheFactory = new DataCacheFactory(factoryConfig);

// Get a cache client for the default cache.
DataCache defaultCache = cacheFactory.GetDefaultCache();

// Add and retrieve a test object from the default cache.
defaultCache.Add("testkey", "testobject");
string strObject = (string)defaultCache.Get("testkey");

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft