title: How do I clear a System.Runtime.Caching.MemoryCache
body: <p>I use a <code>System.Runtime.Caching.MemoryCache</code> to hold items which never expire. However, at times I need the ability to clear the entire cache. How do I do that?</p>
  
  <p>I asked a similar question <a href="https://stackoverflow.com/questions/8023543/can-i-iterate-over-the-net4-memorycache/">here</a> concerning whether I could enumerate the cache, but that is a bad idea as it needs to be synchronised during enumeration.</p>
  
  <p>I've tried using <code>.Trim(100)</code> but that doesn't work at all.</p>
  
  <p>I've tried getting a list of all the keys via Linq, but then I'm back where I started because evicting items one-by-one can easily lead to race conditions.</p>
  
  <p>I thought to store all the keys, and then issue a <code>.Remove(key)</code> for each one, but there is an implied race condition there too, so I'd need to lock access to the list of keys, and things get messy again.</p>
  
  <p>I then thought that I should be able to call <code>.Dispose()</code> on the entire cache, but I'm not sure if this is the best approach, due to the way it's implemented.</p>
  
  <p>Using <code>ChangeMonitors</code> is not an option for my design, and is unnecassarily complex for such a trivial requirement.</p>
  
  <p>So, how do I completely clear the cache?</p>