title: AppFabric Caching - Proper use of DataCacheFactory and DataCache
body: <p>I am looking for the most performant way to arrange usage of the datacache and datacache factory for AppFabric caching calls, for between 400 and 700 cache gets per page load (and barely any puts).  It seems that using a single static DataCacheFactory (or possibly a couple in a round-robin setup) is the way to go.</p>
  
  <p>Do I call GetCache("cacheName") for every DataCache object request, or do I make one static at the time DataCache factory is initialized and use that for all calls?</p>
  
  <p>Do I have to handle exceptions, check for fail codes and attempt retries?</p>
  
  <p>Do I have to consider contention when more than one thread tries to use the cache store and wants the same item (by key)?</p>
  
  <p><strong>Is there some kind of documentation which properly explores the design and usage of this?</strong></p>
  
  <hr>
  
  <p><em><strong>Some information I have gathered so far from the forum:</em></strong></p>
  
  <p><a href="http://social.msdn.microsoft.com/Forums/en-AU/velocity/thread/98d4f00d-3a1b-4d7c-88ba-384d3d5da915">http://social.msdn.microsoft.com/Forums/en-AU/velocity/thread/98d4f00d-3a1b-4d7c-88ba-384d3d5da915</a></p>
  
  <p>"Creating the factory involves connecting to the cluster and can take some time. But once you have the factory object and the cache that you want to work with, you can simply reuse those objects to do puts and gets into the cache, and you should see much faster performance."</p>
  
  <p><a href="http://social.msdn.microsoft.com/Forums/en-US/velocity/thread/0c1d7ce2-4c1b-4c63-b525-5d8f98bb8a49">http://social.msdn.microsoft.com/Forums/en-US/velocity/thread/0c1d7ce2-4c1b-4c63-b525-5d8f98bb8a49</a></p>
  
  <p>"Creating single DataCacheFactory (singleton) is more performing than creating multiple DataCacheFactory. you should not create DataCacheFactory for each call, it will have performance hit."</p>
  
  <p>"Please try to encapsulate round-robin algorithm (having 3/4/5 factory instances) in your singleton and compare load-test results."</p>
  
  <p><a href="http://blogs.msdn.com/b/velocity/archive/2009/04/15/pushing-client-performance.aspx">http://blogs.msdn.com/b/velocity/archive/2009/04/15/pushing-client-performance.aspx</a></p>
  
  <p>"You can increase the number of clients to increase the cache throughput. But sometimes if you want to have smaller set of clients and increase throughput, a trick is to use multiple DataCacheFactory instances. The DataCacheFactory instance creates a connection to the servers (e..g if there are 3 servers, it will create 3 connections) and multiplexes all requests from the datacaches on to these connections. So if the put/get volume is very high, these TCP connections might be bottlenecked. So one way is to create multiple DataCacheFactory instances and then use the operations on them."</p>
  
  <hr>
  
  <p>Here what is in use so far... the property is called and if the return value is not null an operation is performed.</p>
  
  <pre><code>private static DataCache Cache
  {
      get
      {
          if (_cacheFactory == null)
          {
              lock (Sync)
              {
                  if (_cacheFactory == null)
                  {
                      try
                      {
                          _cacheFactory = new DataCacheFactory();
                      }
                      catch (DataCacheException ex)
                      {
                          if (_logger != null)
                          {
                              _logger.LogError(ex.Message, ex);
                          }
                      }
                  }
              }
          }
  
          DataCache cache = null;
  
          if (_cacheFactory != null)
          {
              cache = _cacheFactory.GetCache(_cacheName);
          }
  
          return cache;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>See this question on Microsoft AppFabric forum: <a href="http://social.msdn.microsoft.com/Forums/en-AU/velocity/thread/e0a0c6fb-df4e-499f-a023-ba16afb6614f">http://social.msdn.microsoft.com/Forums/en-AU/velocity/thread/e0a0c6fb-df4e-499f-a023-ba16afb6614f</a></p>