2
votes

I've got our site all setup like this in my web.config:

<dataCacheClients>
    <dataCacheClient name="default">
      <hosts>
        <host name="mysite.cache.windows.net" cachePort="22233" />
      </hosts>

      <securityProperties mode="Message">
        <messageSecurity
          authorizationInfo="{key}">
        </messageSecurity>
      </securityProperties>
    </dataCacheClient>
  </dataCacheClients>

I'd like to edit this so while I'm developing on my laptop I'm not hitting the production cache but instead a cache on my laptop.

This is a hard problem to Google (at least for me) because "azure cache local" comes up with caching on web roles.

2

2 Answers

4
votes

It looks like you are using Windows Azure Shared Cache, and you want to use some local cache when developing.

It might be better to have an abstract cache layer in your system so that you can switch caches between cloud and local, rather than changing the configuration. For example, to have an interface like ICache with some methods like GetItem, SetItem, etc. Then you can have some classes implemented this interface that are using in memory cache for local development, and Azure Cache for production.

There's a project named ServiceStack on GitHub that wrapped some cache implementation you can refer https://github.com/ServiceStack/ServiceStack.Contrib/tree/master/src

Alternatively, you can use the new Cloud Service Caching, which provides co-located/dedicate cache clusters alone with your cloud services (web role and worker role). It has full local emulator support which means you don't need to change any code and any configuration between local development (use local cache emulator) and production.

For more information about this Cloud Service Caching you can refer https://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/

2
votes

One option would be to use Web.config Transformations to change the config file for each deployment. I'm not an expert in that, but you can probably figure it out with Google.

Another option is to configure the cache in code. You can then easily change the settings in the .cscfg file for each environment. Here's a basic sample of configuration in code:

DataCacheFactoryConfiguration cacheConfig = new DataCacheFactoryConfiguration();

//Insert the Authentication Token as shown below
cacheConfig.SecurityProperties = new DataCacheSecurity(config.AuthToken, config.UseSSL);

int cachePort = (cacheUseSSL ? 22243 : 22233);
cacheConfig.Servers = new DataCacheServerEndpoint[] { new DataCacheServerEndpoint(cacheHostName, cachePort) };
cacheConfig.RequestTimeout = TimeSpan.FromSeconds(1);
cacheConfig.ChannelOpenTimeout = TimeSpan.FromSeconds(45);
cacheConfig.MaxConnectionsToServer = config.MaxConnections;
cacheConfig.TransportProperties.ReceiveTimeout = TimeSpan.FromSeconds(30);
cacheConfig.TransportProperties.ChannelInitializationTimeout = TimeSpan.FromSeconds(10);

var cacheFactory = new DataCacheFactory(cacheConfig);