I have a private ConcurrentDictionary
that is a simple lookup table of some DB keys.
I'm trying to leverage the ConcurrentDictionary
so that it will only do one call to the db when 2+ requests to the same line of code, are made at the same time. (Which is why i'm using a ConcurrentDictionary
.)
How can I do this please?
This is what I was trying to do .. but I think it's storing the Task
in the dictionary ... not the result of the task....
private readonly ConcurrentDictionary<string, Task<int>> _myKeys = new ConcurrentDictionary<string, Task<int>>();
...
private async Task<int> DoStuffAsync(string key)
{
// do stuff here.
return await _myKeys.GetOrAdd(key,
async k => await _db.GetId(k)
.ConfigureAwait(false))
.ConfigureAwait(false);
}
Any ideas?
EDIT:
Notice my method signature and what I'm returning. Is it better to return an int
and not a Task<int>
and then somehow refactor my db call to still be async .. but .. better?