Multiple client configurations in single application?
-
Wednesday, September 16, 2009 6:45 PMAre there any plans to support multiple, named client configurations in a single application? The best example I can give is that I want certain caching in my app to use localCache and others to always go direct to server for more volatile data. Right now the only way to do this is to create your own configurations and instantiate DataClients with all the proper settings.
Just curious,
Drew
All Replies
-
Thursday, September 17, 2009 8:46 AM
Drew,
It seems you are using the default constructor of DataCacheFactory class which reads app config file for local cache and other parameters, instead you can use the parameterized constructor of DataCacheFactory class where you can sepcify all the parameters programatically.DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
servers[0] = new DataCacheServerEndpoint("localhost", 22233, "DistributedCacheService");
// DataCacheFactory(Microsoft.Data.Caching.DataCacheServerEndpoint[] servers, bool routingClient, bool localCache)
DataCacheFactory myCacheFactory = new DataCacheFactory(servers, true, false);
defaultCache = myCacheFactory.GetCache("default");Thanks
Shankar- Proposed As Answer by Shankar Sama-MSFT Wednesday, September 30, 2009 6:57 AM
- Unproposed As Answer by Drew Marsh Thursday, October 01, 2009 1:21 AM
-
Thursday, October 01, 2009 1:20 AMSure, I'm aware of this, but it puts all the configuration on the app developer if I have the need for multiple cache characteristics. What I'd like to see is named cache configurations so I can do:
new DataCacheFactory("ShoppingCarts") or new DataCacheFactory("Products")
Where the ShoppingCarts configuration has different settings than thte Products configuration. For example, Products would be long lived and could use local caching, but the ShoppingCarts are volatile and I always want the app to pull the data straight from the cache.
Am I making sense?
Thanks,
Drew -
Thursday, October 01, 2009 5:59 AM
Drew,
You need to create multiple DataCacheFactory objects with different properties to achieve this since the local cache is tied up with the DataCacheFactory object.
Example with pseudo code:
cf1 = new DataCacheFactory(with local cache on);
cache1 = cf1.GetCache ("Products"); // here cache1 uses local cache
cf2 = new DataCacheFactory(with local cache off);
cache2 = cf2.GetCache ("ShoppingCarts"); // here cache2 does not use local cache
Thanks
Shankar -
Monday, October 12, 2009 8:13 PMShankar, you're not really reading (or answering) the original question right.
What Drew really wants is a way to have multiple client-configurations in the app.config / web.config file. In CTP 3, there's only support for a single client configuration - which is a pity, because it's a mess to embed these configuration settings within your application and since it's a trivial task to expand the XML schema to allow for multiple client configurations, it should be easy for you to support this important part of the API (I'm surprised why the current design was accepted, since this is a common pattern).
Unless addressed, Drew (and basicaly all other developers using Velocity) would have to store the configuration settings in his own config-section in the app.config / web.config. Please forward this to the team - thanks in advance.
Anders Borum / SphereWorks -
Wednesday, October 21, 2009 7:04 PMModerator
Thanks for your question and feedback. This is something which we have been pondering about and plan to discuss for V1. However it is not very clear if this will eventually make it to V1 or not.HTH.
Ankur- Marked As Answer by Ankur Agrawal-MSFTModerator Wednesday, October 21, 2009 7:04 PM
-
Monday, October 25, 2010 8:52 PM
I'm also trying to implement an app that allows some objects to be cached locally and others not to be. It's clear that the only way to do this is to use two separate DataCacheFactory instances, one configured to use local cache and the other configured not to. In my scenario, that's the only thing I want configured differently between the two factories, and I want to be able to change my cache client configuration by changing my web.config. Is there any way to override just that setting on the configuration before instancing the factory based on an XML config?
I would really like to be able to do this:
_localFactory = new DataCacheFactory(); // app config has localCache enabled var remoteFactoryConfig = DataCacheFactoryConfiguration.LoadFromAppConfig(); remoteFactoryConfig.LocalCacheProperties.IsEnabled = false; _remoteFactory = new DataCacheFactory(remoteFactoryConfig);
The documentation says:
"Because programmatic configuration of the cache client overrides the XML settings, you may also want to take a hybrid approach. Settings in the application configuration file could be treated as your application's default and you could override those settings with programmatic configuration."
This is exactly what I want to do, but the API appears to make this difficult. Is there a way I can override just a few settings from an XML configuration when instancing a factory?
-
Thursday, May 19, 2011 9:48 PM
So I revisited this issue today and discovered that the constructor for the DataCacheFactoryConfiguration class reads in the settings from the app config by default. This is completely undocumented behavior, but VERY useful in my case. I need two DataCacheClientFactories, one that uses local cache and another that does not, so I can ensure near-real time invalidation for some cached items, but leave others in local cache that can afford to be stale for a short time for less latency. Here's a simple example class that wraps up this behavior. I hope this helps someone, I spent far too much time tying to figure this out...
public static class AppFabricCacheManager { private const string CACHE_NAME = "default"; private static readonly DataCacheFactory RemoteCacheFactory; private static readonly DataCacheFactory LocalCacheFactory; static AppFabricCacheManager() { RemoteCacheFactory = new DataCacheFactory(); var localCacheFactoryConfig = new DataCacheFactoryConfiguration(); localCacheFactoryConfig.LocalCacheProperties = new DataCacheLocalCacheProperties(100000, new TimeSpan(0,5,0), DataCacheLocalCacheInvalidationPolicy.TimeoutBased)); LocalCacheFactory = new DataCacheFactory(localCacheFactoryConfig); } public static void Put(string key, object value, bool localCacheAllowed) { GetCache(localCacheAllowed).Put( key, value ); } public static object Get(string key, bool localCacheAllowed) { return GetCache(localCacheAllowed).Get(key); } public static void Remove(string key, bool localCacheAllowed) { GetCache(localCacheAllowed).Remove(key); } private static DataCache GetCache(bool localCacheAllowed) { if(localCacheAllowed) { return LocalCacheFactory.GetCache(CACHE_NAME); } return RemoteCacheFactory.GetCache(CACHE_NAME); } }
And be sure to leave local caching out of the config file
<dataCacheClient> <hosts> <host name="localhost" cachePort="22233" /> </hosts> </dataCacheClient>
-
Wednesday, January 18, 2012 6:03 PM
For anyone who is coming across this post, AppFabric 1.1 added support for multiple client configs.
- Marked As Answer by Drew Marsh Wednesday, January 18, 2012 6:03 PM

