Tracking Online Users With Redis

I want to track the number of users on the network, I use Redis to solve this problem, since one or more application instances access this cache to display statuses. With each new login, Redis is updated with the user ID, on the key "online_users" and at each logout the user is deleted. Is this approach right if not how to structure it for better performance, but a somewhat large data set?

"online_users" -> {
     user_s23,user_1f3,user_1mn,user_xd3
}

User information is available in the cache as, is it correct to include the status as another attribute?

user_s23 {
name, id, profile_pic, type, etc, status:active/inactive
}

Although this 2 is suitable for tracking online users using Redis. Which one is faster? The answer to the question compares two approaches to this, the first link mentioned is not available, so I could not understand another context.

+4
source share
2 answers

If you only want to know:

1- How many users are online

2- What are the users on the network

3- Find if this user is online

, , , set , ( , ).

:

  • user_a :

> sadd online_users user_a

( ) 1

  • user_b:

> sadd online_users user_b

( ) 1

  • ?

> scard online_users

( ) 2

  • user_a?

> sissember online_users user_a

( ) 1

  • user_a :

srem online_users user_a

( ) 1

  • ?

online_users

1) "user_b"

- O (1), smembers, O (N), - , . - , , Redis .

, , "2 - Redis. ?".

, ( ) , : online_users_2014-02-20_10-01 (- 10:01 02/20/2014).

, , , , , 3 . 3- scard , , .

, sunion - O (N). - ( expire) , .

, , , , PING , , , .

+10

1)

, , bitset, , , Redis ( , , , getbit) .

, .

:

1 - 43

> setbit online_users 43 1

( ) 0

2 - 57

> setbit online_users 57 1

( ) 0

3 -

bitcount online_users

( ) 2

4 - 43

> setbit online_users 43 0

( ) 1

5 -

bitcount online_users

( ) 1

6 - , 57

> getbit online_users 57

( ) 1

2) , , , , ( getbit).

3) here

, , .

, , KEYS, , .

BITCOUNT Redis.

+4

Source: https://habr.com/ru/post/1527691/


All Articles