What I'm trying to do is to keep online members of group chats in memory. I've defined a static nested dictionary like this:
private static ConcurrentDictionary<string, ConcurrentDictionary<string, ChatMember>> onlineGroupsMembers = new ConcurrentDictionary<string, ConcurrentDictionary<string, ChatMember>>();
Then when a new member arrives, I add it:
onlineGroupsMembers.AddOrUpdate
(chatKey,
(k) => // add new
{
var dic = new ConcurrentDictionary<string, ChatMember>();
dic[chatMember.Id] = chatMember;
return dic;
},
(k, value) => // update
{
value[chatMember.Id] = chatMember;
return value;
});
Now the problem is how can I delete a member from the inner dictionary? also how to delete a dictionary from outer dictionary when it's empty?
Concurrent dictionary has TryRemove but it does not help and checking for ContainsKey then removing it is not atomic.
Thanks.
TryRemove()?keywhich in my case is group key, and removes the value which is a dictionary of that group members. I just want to remove a single member, not all.TryGetValueto extract the inner dictionary, and then aTryRemoveon the inner dictionary.ChatMembers or member IDs, and another that maps from member IDs toChatMembers.