I have the following class:
public class MatchMaker : Hub
{
private readonly ISoloUserRepository soloUsers;
public MatchMaker(ISoloUserRepository userRepo)
{
this.soloUsers = userRepo;
}
//Client Requests
public void findNewPartner(string Name, string Country)
{
SoloUser soloUser = soloUsers.Users.FirstOrDefault(s => (s.Name == Name) && (s.Country == Major));
if (soloUsers.Users.Count > 0){
Clients.partnerRequestResult(soloUsers.Users.FirstOrDefault());
soloUsers.Users.Remove(soloUser);
}
else
{
soloUser = new SoloUser {
Name = Name,
Country = Country
};
soloUsers.Users.Add(soloUser);
}
}
}
I want to be able to keep my soloUsers object in memory, so whenever this class is accessed it keeps track of the SoloUser objects that are added to the soloUsers repository. I havent quite built out my SoloUserRepository class, so I wanted to know the best way to handle this. Would I have to use a database in this sense to maintain the SoloUser being added to the collection, or is there a way to store this collection in memory? Keep in mind that users are constantly being added and removed.
I’d say that it doesn’t matter if the collection is large or small. It depends on other factors, such as:
Do you require the collection to persist if your process shuts down? If not – memory will be OK; if it is OK that you loose only some data – synchronize memory with persistent storage from time to time; otherwise – use (not necessarily database) storage.
How many threads will be accessing your collection? This is some backend to web/cs application, right? If that is so – then you will need a few locks and events to synchronize access. Relational database can usually handle these for you.
If you are sure, that collection will remain small – memory will be OK. What happens if it grows? Would it be easier to move part of process to other server or move persistent storage to other server? Will your routines remain fast enough?