Use Redis to count online active users

  • 2020-05-14 05:22:29
  • OfStack

preface

At work we often encounter the need to count the number of active users of an online site. Here we take redis as an example to illustrate the implementation process of 1.

Implementation method

The bitmap data type exists in Redis, which is based on the string data type. Here, we mainly use the two commands setbit and bitcount, and use the redis library of python as the client.


import redis 
r = redis.StrictRedis(host="127.0.0.1",port=6379,db=0)

Here we introduce the redis library and instantiate an StrictRedis class. Because of the default option used here, we can pass no parameters in StrictRedis.


r = redis.StrictRedis()

Here we define three methods, storeDailyVisit,removeDailyVisit, and countVisits, for recording when a user goes online, recording when a user goes offline, and viewing the total number of active users on a given date.

Let's look at the storeDailyVisit function:


def storeDailyVisit(date,userId,verbose=False): 
 key = "visits:daily:"+date 
 r.setbit(key,userId,1) 
 if verbose: 
  print("User",userId,"visited on",date)

This function receives two parameters, date and userId, for the specified date and ID for the user, respectively, and the third parameter is used to specify whether or not to output content. Here we output the information that the user has accessed, and of course we can log it for other operations.

When a user logs in, we call this function, passing in today's date and the user's ID. The removeDailyVisit function is similar to the storeDailyVisit function in that it is responsible for removing users from the bitmap table when they exit the current site.


def removeDailyVisit(date,userId,verbose=False): 
 key = "visits:daily:"+date 
 r.setbit(key,userId,0) 
 if verbose: 
  print("User",userId,"leave on",date)

Below, we get the number of active users through the countVisits function:


def countVisits(date): 
 key = "visits:daily:"+date 
 return r.bitcount(key)

Here, the countVisits function receives a date argument, and it gets the current number of 1 by calling the bitcount function of the bitmaps data type. Here we return it to the caller.

Since bitmaps is a bitwise data type, it is a string of consecutive base 2 digits (0 or 1). We get the total number of active users by setting the value of one of its bits to 1, and then counting the number of 1's in the base 2 through the bitcount function.

In addition, we can also use redis's set data type to achieve the same operation. We can see the differences between these two by the following table:


|  The data type  |  Each bit consumed  |  Storing user  |  Memory consumption  |
| -- - -| -- -- - | -- -- -- | -- -- -- |
| bitmap | 1bit | 500000000 |1*500000000B=59.6Mb|
| set | 32bit | 500000000 |32*500000000B=1.91Tb|

We can clearly see that when the number of storage users is 500 million, the required memory consumption of bitmap is 59.6MB, and set is 1.91TB, a full 32 times. Therefore, in the actual online website, we use bitmaps more to count the number of online users.

Refer to the article: http: / / redis io/topics data types -- intro # bitmaps

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: