title: What are best practices / good patterns for managing cached async data?
body: <p>I am rewriting an old app and I am trying to use <code>async</code> to speed it up.</p>
  <p>The old code was doing something like this:</p>
  <pre><code>var value1 = getValue(&quot;key1&quot;);
  var value2 = getValue(&quot;key2&quot;);
  var value3 = getValue(&quot;key3&quot;);
  </code></pre>
  <p>where the <code>getValue</code> function was managing its own cache in a dictionary, doing something like this:</p>
  <pre><code>object getValue(string key) {
    if (cache.ContainsKey(key)) return cache[key];
    var value = callSomeHttpEndPointsAndCalculateTheValue(key);
    cache.Add(key, value);
    return value;
  }
  </code></pre>
  <p>If I make the <code>getValue</code> <code>async</code> and I <code>await</code> every call to <code>getValue</code>, then everything works well. But it is not faster than the old version because everything is running synchronously as it used to.</p>
  <p>If I remove the <code>await</code> (well, if I postpone it, but that's not the focus of this question), I finally get the slow stuff to run in parallel. But if a second call to <code>getValue(&quot;key1&quot;)</code> is executed before the first call has finished, I end up with executing the same slow call twice and everything is slower than the old version, because it doesn't take advantage of the cache.</p>
  <p>Is there something like <code>await(&quot;key1&quot;)</code> that will only await if a previous call with <code>&quot;key1&quot;</code> is still awaiting?</p>
  <p><strong>EDIT</strong> (follow-up to a comment)</p>
  <p>By &quot;speed it up&quot; I mean more responsive.</p>
  <p>For example when the user selects a material in a drop down, I want to update the list of available thicknesses or colors in other drop downs and other material properties in other UI elements. Sometimes this triggers a cascade of events that requires the same <code>getValue(&quot;key&quot;)</code> to used more than once.</p>
  <p>For example when the material is changed, a few functions may be called: <code>updateThicknesses()</code>, <code>updateHoleOffsets()</code>, <code>updateMaxWindLoad()</code>, <code>updateMaxHoleDistances()</code>, etc. Each function reads the values from the UI elements and decides whether to do its own slow calculations independently from the other functions. Each function can require a few http calls to calculate some parameters, and some of those parameters may be required by several functions.</p>
  <p>The old implementation was calling the functions in sequence, so the second function would take advantage of some values cached while processing the first one. The user would see each section of the interface updating in sequence over 5-6 seconds the first time and very quickly the following times, unless the new value required some new http endpoint calls.</p>
  <p>The new async implementation calls all the functions at the same time, so every function ends up calling the same http endpoints because their results are not yet cached.</p>