title: Lock ConcurrentDictionary while removing and adding multiple items?
body: <p>I have read the following articles on StackOverflow: <a href="https://stackoverflow.com/questions/10177768/concurrentbag-add-multiple-items">ConcurrentBag - Add Multiple Items?</a> and <a href="https://stackoverflow.com/questions/8225775/concurrent-dictionary-correct-usage">Concurrent Dictionary Correct Usage</a> but the answers are still somehow not obvious to me.</p>
  
  <p>I have this scenario: I have <code>Leaderboard</code> table in the database and I update it periodically. To optimze the server, I cache the result, so I use ConcurrentDictionary (because there are different types of leaderboards, such as All-time, 3-days, 7-days, etc...).</p>
  
  <p>Here are my code at the updating leaderboard:</p>
  
  <pre><code>        var leaderboards = business.UpdateLeaderboard(LeaderboardUpdater.LeaderboardDaySpans, LeaderboardUpdater.LeaderboardCount);
          this.LastUpdateTime = now;
  
          // The LeaderboardCache is ConcurrentDictionary&lt;int, LeaderboardResponseViewModel&gt;
          this.LeaderboardCache.Clear();
          foreach (var leaderboard in leaderboards)
          {
              this.LeaderboardCache.TryAdd(leaderboard.DaySpan, new LeaderboardResponseViewModel(leaderboard));
          }
  </code></pre>
  
  <p>Assume the user may request Leaderboard information at any time. So I have some questions:</p>
  
  <ul>
  <li>Should I use <code>Concat</code> instead of <code>foreach</code> to ensure all items are added at the same time?</li>
  <li>Even if I use <code>Concat</code>, how can I ensure that the user won't request at the middle of the <code>Clear</code> and <code>Concat</code> method?</li>
  <li>Should I apply an additional lock? If so, how can I ensure concurrent read, since multiple read at the same time is okay?</li>
  </ul>