title: How to implement periodic cache reload
body: <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 above cache is used by a singleton wrapper. The following is the implementation. </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>Now my question is if I want to reload cache, can i use the following? I want to make the reload seamless, so that its OK to have threads access stale information.</p>
  
  <pre><code>public void Reload(IDictionary&lt;TKey, TValue&gt; values)
  {
      cache = new Dictionary&lt;TKey, TValue&gt; ();
      foreach (KeyValuePair&lt;TKey, TValue&gt; value in values)
      {
          cache[value.Key] = value.Value;
      }
  
      lock (_syncLock)
      {
          _internalCache = cache;
      }
  }
  </code></pre>
  
  <p>_internalCache is readonly. It won't let me change assignment.</p>