Azure, Coding, How to

Configure Azure Cache From Cloud Config in PAAS

I’ve recently been working with the Azure cache  client which, by default, is configured by storing the details in your app.config or web.config.

Now this is great if you’re in the traditional world but for Azure Platform as a service solutions Cloud Configuration is key. It allows you to change settings easily across multiple machines without having to jump into web.configs or redeploy etc.

I wrote the little method below to help me to do this as in my testing I was jumping between a couple of different cache’s regularly.

/// <summary>
        /// Wrapper for the cache factory to create a DataCache from Authentication token and discovery url
        /// </summary>
        /// <param name="token"></param>
        /// <param name="discoveryurl"></param>
        /// <returns></returns>
        private DataCache CacheFactoryWrapper(string token, string discoveryurl)
            DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();
            config.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, discoveryurl);
            if (!String.IsNullOrEmpty(token))
                config.SecurityProperties = new DataCacheSecurity(token);
            DataCacheFactory cacheFactory = new DataCacheFactory(config);
            DataCache cache = cacheFactory.GetDefaultCache();
            return cache;

You can then use the CloudConfigurationManager to retreive the info needed then call into the method to start using the cache:


You may want to alter to use something other than the “GetDefaultCache” method but hopefully a good starting point.

[This method was tested in a limited way, double check it makes sense for you before going nuts and putting it into prod!]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s