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 rather expensive method with some complex database calls, and we have a general situation where hundreds of users with the same AccountId will call almost simultaneously (they are all notified of broadcast transmission).
In the method, we cache the result set for 10 seconds, since a result close to time is suitable for everyone who makes a request in this window. However, since they all make a call at the same time (again, for a specific account identifier), the cache is never populated in front, so everyone ends up calling the database.
So my question is, how can I suspend all incoming requests for a specific account within the method 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 got a little familiar with Monitor.Pulse and Monitor.Lock, but the implementation for locking for each account eludes me. Any help would be greatly appreciated.
Feech source share