We have a REST API method similar to:
List<item> GetItems(int AccountID)
{
var x = getFromCache(AccountID);
if(x==null)
{
x = getFromDatabase(AccountID);
addToCache(AccountID, x);
}
return x;
}
This is a fairly costly method with some complicated DB calls, and we have a common situation where hundreds of users with the same AccountId will make the call almost simultaneously (they are all being notified with a broadcast).
In the method, we cache the result set for 10 seconds since a near-time result is fine for everyone making the request within that window. However, since they all make the call at the same time (again, for a specific AccountID) the cache is never populated up front, so everyone ends up making the database call.
So my question is, within the method, how can I pause all incoming requests for a specific accountId and make them all wait for the first result set to complete, so that the rest of the calls can use the cached result set?
I've read a little about Monitor.Pulse and Monitor.Lock but the implementation for a per-accountId lock kind of escapes me. Any help would be tremendously appreciated.