I am rewriting an old app and I am trying to use async
to speed it up.
The old code was doing something like this:
var value1 = getValue("key1");
var value2 = getValue("key2");
var value3 = getValue("key3");
where the getValue
function was managing its own cache in a dictionary, doing something like this:
object getValue(string key) {
if (cache.ContainsKey(key)) return cache[key];
var value = callSomeHttpEndPointsAndCalculateTheValue(key);
cache.Add(key, value);
return value;
}
If I make the getValue
async
and I await
every call to getValue
, then everything works well. But it is not faster than the old version because everything is running synchronously as it used to.
If I remove the await
(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 getValue("key1")
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.
Is there something like await("key1")
that will only await if a previous call with "key1"
is still awaiting?
Aucun commentaire:
Enregistrer un commentaire