简体   繁体   English

静态列表<User> VS Asp.Net中的在线用户数据库?

[英]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 . 我正在构建ASP.Net MVC应用程序“kinda Game”,它与在线用户打交道很多

I made Ajax request fired every "10s" to some Action to keep user online when he keeps site open. 我让Ajax请求每个"10s""10s"一些Action ,以便在用户保持站点打开时保持用户在线。 this Action update LastActivityDate for this User - ((in static List and DataBase)). Action为此User 更新 LastActivityDate - ((在静态列表和数据库中))。


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. 将在线用户存储在静态列表中并在用户登录时将一些代码添加到该列表然后每隔"10s"保持管理此列表以启动离线用户更好。
  • Or the best is to load online users from DataBase whenever i want OnlineUsers . 或者最好是每当我想要OnlineUsers从DataBase加载在线用户。


note: I'm using technique from this SO Question to do some periodically tasks like re-manage OnlineUsers static list. 注意:我正在使用此SO问题中的技术来定期执行一些任务,例如重新管理OnlineUsers静态列表。

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. 首先,您不会使用List<User> ,而是使用用户的id作为键来使用Dictionary<int,User> ,这样您就可以立即找到要更新的用户。 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 . 如果您打算拥有大量用户,并且需要经常从数据库中提取数据,那么最好将用户列表存储在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. static的范围始终是在操作系统中运行的进程的范围。 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. 这是执行此操作的Web服务器。 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. 您仍然可以将静态范围作为临时缓存,但不能依赖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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