title: ObjectCache and how it works
body: <p>Following c# code is a stripped version of what I have. This basically cache data using <code>memory cache</code> and the <code>AbsoluteExpiration</code> values decides when to clear the cache. All works as expected. However, recently, I had to include following code within a <code>singleton</code> data structure. This left me with some questions;</p>
  
  <ol>
  <li>how/who initiate cache clearing
  process when cache expire?</li>
  <li><p>how it works in multi-threaded scenario,
  when my singleton object is accessed parallely?</p>
  
  <blockquote>
  <pre><code>ObjectCache cache = MemoryCache.Default;
  if (cache.Contains(”UniqueKey”))
  return cache.Get(”UniqueKey”);    
  var dataToCache = GetData();//This is the data that I cache
  var policy = new CacheItemPolicy {AbsoluteExpiration = DateTime.Now.AddMinutes(int.Parse(ConfigurationManager.AppSettings["CacheExpiry"]))};
  cache.Add(”UniqueKey”, dataToCache, policy);
  return dataToCache;
  </code></pre>
  </blockquote></li>
  </ol>