title: How to clear ASP.NET cache thread-safely?
body: <p>In my project I have some cached values implemented using singleton pattern - it looks like this:</p>
  
  <pre><code>Roles GetRoles
  {
  get{
           var cached = HttpContext.Current.Cache["key"];
           if(cached == null){
               cached = new GetRolesFromDb(...);
           }
           return cached as Roles;
     }
  }    
  </code></pre>
  
  <p>When I change the roles I'm clearing the cache (iterating over all keys).
  I think it isn't thread-safe - if some request tries to get cached roles, 
  cached != null and meanwhile cache had been cleared GetRoles returns null.</p>