title: Cache object with ObjectCache in .Net with expiry time
body: <p>I am stuck in a scenario.
  My code is like below :</p>
  
  <p><strong>Update : its not about how to use data cache, i am already using it and its working , its about expanding it so the method don't make call between the time of expiry and getting new data from external source</strong></p>
  
  <pre><code>object = (string)this.GetDataFromCache(cache, cacheKey);
  
  if(String.IsNullOrEmpty(object))
  {
    // get the data. It takes 100ms
    SetDataIntoCache(cache, cacheKey, object, DateTime.Now.AddMilliseconds(500));
  }
  </code></pre>
  
  <p>So user hit the cache and get data from it if the item expire it calls and get the data from service and save it in case , t<strong>he problem is , when ever there is a pending request ( request ongoing  ) the service send another request because the object is expired  . in final there should be max 2-3 calls/ seconds and there are 10-20 calls per seconds to external service .</strong> </p>
  
  <p>Is there any optimal way to doing it so no conflict between requests time other then creating own custom class with arrays and time stamps etc?</p>
  
  <p>btw the saving code for cache is-</p>
  
  <pre><code>private void SetDataIntoCache(ObjectCache cacheStore, string cacheKey, object target, DateTime slidingExpirationDuration)
  {
    CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
  
    cacheItemPolicy.AbsoluteExpiration = slidingExpirationDuration;
    cacheStore.Add(cacheKey, target, cacheItemPolicy);
  }
  </code></pre>