I know how many users are online, I use Redis to solve this problem because one or more In order to display the number of application examples using this example, Redis is updated with the user "id" on each new login, on the key "online_users", and the user is removed in every logout. Is this approach right, if not, how to prepare it for the best performance, and some big datasets?
"online_users" - & gt; {User_s23, user_1f3, user_1mn, user_xd3}
User information is available in the cache, is it right to include the status as another attribute?
user_s23 {name, id, profile_pic, type, etc., status: active / inactive}
However, the answer to this question is that it compares two ways The first link mentioned is unavailable, so I could not understand the second reference.
If you only want to know
1- how many users are online
2- Which users are online
3- Find out if a user is online or not
and you can make sure that you every Logout and all users can call the logout request, then you can use set
and the user can archive the user login Is when the user logs out (the same as the way you've described).
Example:
- Login to user_a:
> sadd online_users user_a
( Integer) 1
- user_blogin:
> sadd online_users user_b
(integer) 1
- How many users are online?
enter online_aners
(integer) 2
- Is user_a online?
- user_a logout:
sismember online_users user_a
(integer) 1
> srem online_ user user_
(integer) 1
Blockquote>All of these O (O) except Operation O (1)
smembers
, your only concern and limit is the memory used to store these data. An indication is that your users are trying to use a small key, if you can not, Radis can already insert them.If you can not be sure that you will track the logout, then you can use the second approach to the question "2 views to keep track of online users with Radis. ? ".
To use this approach, you have to track all the user's events (not just login), and store the User ID on different keys using the time stamp, like: online_users_2014 -02-20_10-01 (Online user on 02/20/2014 at 10:01 PM).
To answer how many users are currently online, you can say, for example, that the user is considered online if he has sent at least one event in the last 3 minutes. Then you can calculate the union of the last 3 sets and by using the scraps on the resulting set, find out how many users are online.
This approach is very expensive, because
sunion
is O (N).Another approach based on the previous one, is to add something, and it is a good idea to add timeouts (using the
expired
command) on your keys. But the logic, which sends every ping in the background to your backend, you can track this event and you will ensure that users of that set are online.
Comments
Post a Comment