title: C# reload singleton cache
body: <p>I need some directions here.
  I have the following key/value cache:</p>
  
  <pre><code>public class Cache&lt;TKey, TValue&gt; : ICache&lt;TKey, TValue&gt;
  {
      private readonly IDictionary&lt;TKey, TValue&gt; _internalCache;
      private readonly object _syncLock = new object();
  
      public Cache()
      {
          _internalCache = new Dictionary&lt;TKey, TValue&gt;();
      }
  
      public TValue this[TKey key]
      {
          get
          {
              lock (_syncLock) {
                 //...
              }
          }
          set
          {
              lock (_syncLock) {
                 //...
              }
          }
      }
  
      public ICollection&lt;TValue&gt; GetAll()
      {
          lock (_syncLock) {
              return _internalCache.Values;
          }
      }
  
      public bool ContainsKey(TKey key)
      {
          lock (_syncLock)
          {
              return _internalCache.ContainsKey(key);
          }
      }
  
  }   
  </code></pre>
  
  <p>The cache above is used by a singleton wrapper:</p>
  
  <pre><code> public class ActivityCache : ICache&lt;string, Activity&gt; 
   {
      private readonly ICache&lt;string, Activity&gt; _cache = new Cache&lt;string, Activity&gt;();
  
      private static readonly ActivityCache _instance = new ActivityCache();
  
      // http://www.yoda.arachsys.com/csharp/singleton.html
      static ActivityCache()
      {
      }
  
      ActivityCache()
      {
      }
  
      public static ActivityCache Instance
      {
          get { return _instance; }
      }
  
      public Activity this[string activityUrl]
      {
          get
          {
              if (string.IsNullOrEmpty(activityUrl))
              {
                  return null;
              }
  
              return _cache[activityUrl];
          }
          set
          {
              if (string.IsNullOrEmpty(activityUrl))
              {
                  return;
              }
  
              _cache[activityUrl] = value;
          }
      }
  
      public ICollection&lt;Activity&gt; GetAll()
      {
          return _cache.GetAll();
      }
  
      public bool ContainsKey(string key)
      {
          return _cache.ContainsKey(key);
      }
  }
  </code></pre>
  
  <p>This is working fine (I haven't noticed/heard of any errors... yet :) ). </p>
  
  <p>But now I have a problem. I need to reload the cache with new key/values. </p>
  
  <p><strong>Question 1.)</strong> Can I implement a "safe" reload method that reloads the cache (the Dictionary in the Cache class) ? </p>
  
  <p>E.g:</p>
  
  <pre><code>    public void Reload(IDictionary&lt;TKey, TValue&gt; values)
      {
          lock (_syncLock)
          {
              _internalCache.Clear();
              foreach (KeyValuePair&lt;TKey, TValue&gt; value in values)
              {
  
                  /* Problems can (will) occur if another 
                     thread is calling the GetAll method... */
                  _internalCache[value.Key] = value.Value;
              }
          }
      }
  </code></pre>
  
  <p><strong>Question 2.)</strong> Should I use some IoC container or some other library instead? </p>
  
  <p>Thanks!</p>
  
  <p><strong>Note:</strong> I'm using .NET 3.5</p>