Hi StackOverflow Community,
- I want to update the user details only once in DB and we are making sure that in Application layer.
- If there are multiple request with same userId, we want to update only with only one request and rest are discarded.
- If we are taking lock then this lock should not be per user nor whole Concurrent dictionary.
- If we are taking lock then this lock should have timeout.
- If there is another subsquent update within 5 minutes we should reject this request.
- Let us assume that all users can fit in one ConcurrentDictionary.
This approach is failing - since multiple threads can pass through if and return True
but only one should pass through and rest should return False
bool ShouldUpdateUser(Guid userId){
TimeInTicks t = LastSeenConcurrentDictionary[userId];
if(DateTime.UtcNow.Ticks - t < 5 minutesTicks){
return false;
}
LastSeenConcurrentDictionary[userId] = DateTime.Now.Ticks;
return True;
}
Another thing which I was thinking is to have another concurrentDictionary which will have lock Object per user and we will do double check locking for updating the timeStamp.
Any Comments would be appreciated. Thanks !