title: How to use MemoryCache insted of Timer to trigger a method?
body: <p>The following method handle concurrent request by waiting the result of an already running operation.</p>
  
  <p>Requests for data may come in simultaneously with same/different credentials. For each unique set of credentials there can be at most one <code>GetCurrentInternal</code> call in progress, with the result from that one call returned to all queued waiters when it is ready.</p>
  
  <pre><code>private readonly ConcurrentDictionary&lt;Credentials, Lazy&lt;Data&gt;&gt; _dataToCredentialMap =
          new ConcurrentDictionary&lt;Credentials, Lazy&lt;Data&gt;&gt;();
  
  public virtual Data GetCurrent(Credentials credentials)
  {
      if (credentials == null) { return GetCurrentInternal(null); }
  
      // It will only allow a single call to GetCurrentInternal, even if multiple threads query its Value property simultaneously.
      var lazyData = new Lazy&lt;Data&gt;(() =&gt; GetCurrentInternal(credentials));
  
      var data = _dataToCredentialMap.GetOrAdd(credentials, lazyData);
      return data.Value;
  }
  </code></pre>
  
  <p>And I have added <code>timer</code> inside of this class in constructor. It's time-based invalidation policy where cache entries are auto-invalidated after a certain well-defined period of time.</p>
  
  <pre><code>_dataUpdateTimer = new Timer(UpdateData, null, TimeSpan.Zero, _dataUpdateInterval); // 1 min
  </code></pre>
  
  <p>Method that update data looks like the following:</p>
  
  <pre><code>private void UpdateData(object notUsed)
  {
      try
      {
          foreach (var credential in _dataToCredentialMap.Keys)
          {
              var data = new Lazy&lt;Data&gt;(() =&gt; GetCurrent(credential));
              _dataToCredentialMap.AddOrUpdate(credential, data, (k, v) =&gt; data);
          }
       }
       catch (Exception ex)
       {
            _logger.WarnException(ex, "Failed to update agent metadata");
       }
  }
  </code></pre>
  
  <p>I would like to use .Net <code>MemoryCache</code> class insted of my <code>ConcurrentDictionary</code> and <code>Timer</code>, to update my <code>Credential and Data</code> I think it will be more efficient.</p>
  
  <p>I know how I can use <code>MemoryCache</code> instead of <code>ConcurrentDictionary</code>, but how can I call <code>UpdateData</code> each minute in constructor without a <code>Timer</code>?</p>
  
  <p>Can you help me please how to do that?</p>