I’m building ASP.Net MVC application “kinda Game” which deal a lot with online users.
I made Ajax request fired every "10s" to some Action to keep user online when he keeps site open.
this Action update LastActivityDate for this User – ((in static List and DataBase)).
So the Question is :
- Is it better to store Online Users in static list and write some code when user log in to add him to that list and then keep manage this list every
"10s"to kick out the offline users. - Or the best is to load online users
from DataBase whenever i wantOnlineUsers.
note: I’m using technique from this SO Question to do some periodically tasks like re-manage OnlineUsers static list.
First, you wouldn’t use a
List<User>for this, but rather aDictionary<int,User>, using the user’s id as the key, so that you could immediately find the user to update. Second, I think it’s probably a mixture of both. You keep a cached copy of the current users, periodically refreshed from the DB, and persist the data (asynchronously, if necessary) to the DB. You might want to think about a custom persistence class for Users that encapsulates this behavior if you find that you’re doing this sort of operation in various places in your code.