简体   繁体   中英

Static list<User> VS DataBase for Online Users in Asp.Net?

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM