3

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 want OnlineUsers.


note: I'm using technique from this SO Question to do some periodically tasks like re-manage OnlineUsers static list.

Community
  • 1
  • 1
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106

3 Answers3

2

First, you wouldn't use a List<User> for this, but rather a Dictionary<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.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
2

If you intend on having a large number of users, and you would need to pull data from the DB frequently, it may be better to store a list of users in the Cache. Obviously this will be stored in the server's memory, so you wouldn't want to store a large amount of objects, but if it's just a simple list of online users it shouldn't be an issue.

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
  • 2
    Probably in the Cache, not the Session, since the Session is per-user and you wouldn't want to store a copy of all of the users in each user's Session. Also, LastActivityDate likely has more uses than just the current list of users so you'd need to persist the value eventually. – tvanfosson Feb 27 '11 at 16:00
  • But the static class or list is share between all users without need to take care about cache expire time "or session". So why not just use it ?? – Wahid Bitar Feb 27 '11 at 16:11
1

The scope of static is always the scope of the process that runs in the operating system. So in a desktop application the use of static makes sense. However, I find the use of static a little bit arbitrary for server side applications because you don't control the processes. It's the web server that does this. What if the process ends unexpectedly? Or what if there are many processes that serve your application?

So, the use of the database is unavoidable. Still, you can the static scope as a temporary cache but you cannot rely on it.

linepogl
  • 9,147
  • 4
  • 34
  • 45
  • I'll try to put some logic to reload online list if it's crashed is this enough ?? – Wahid Bitar Feb 27 '11 at 16:08
  • 1
    Yes, of course it's good. The only other thing to pay attention is scaling. If your application requires more than one server (that is more than one processes) then static scope is out of the question. But as long as one server is enough there will be no problem. – linepogl Feb 27 '11 at 16:12