Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Redis count active users with bitmaps?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

First of all, let's take a look at a scenario: a website that needs to count the number of users who have logged in continuously in a week, and the number of users who have logged in in a month.

If you use a traditional database such as Mysql to implement, it is difficult to do so. But if you use Redis to do it, it's very simple. Both the collection type and the Bitmap type of Redis can be easily done. Today, we will mainly talk about how to use Bitmaps to achieve the function of statistical active users.

Bitmaps

In a computer system, the smallest unit of information is bytes, 1 byte equals 8 bits, and each bit can only be 0 or 1 (the computer only knows these two numbers). Using Bitmaps, you can manipulate bits directly.

Think of bigmaps as an array, where each bit can only be 0 or 1, and the subscript of the array is seen here as an offset.

Let's introduce a few commands related to Bitmaps:

Setbit

Setbit key offset value: set a value for the corresponding bit

For example, if users 3, 8, 23 and 32 visited the website today, then

Setbit user:view:2020-5-17 3 1setbit user:view:2020-5-17 8 1setbit user:view:2020-5-17 23 1setbit user:view:2020-5-17 32 1

Development Tip: many applications of id do not start with 1, and many start with specified numbers, such as 1001 and 10001. For these, we can subtract the initial value when setting it to prevent waste of space.

Getbit

Getbit key offset gets the value of the location

If I want to know if users 8 and 45 have logged in today, then

127.0.0.1 getbit user:view:2020 6379 > getbit user:view:2020-5-17 8 (integer) 1127.0.1 getbit user:view:2020-17 45 (integer) 0

You can see that user 8 logged in today, but user 45 hasn't logged in yet.

Bitcount

Bitcount key [start] [end] gets the number of specified ranges of 1

I want to know how many users have logged in today, then

127.0.0.1 6379 > bitcount user:view:2020-5-17 (integer) 4

Operation between Bitmaps

Bitop op destkey key [key...]

The bitop command can and, or, not, or xor multiple bitmaps, and store the operation results in destkey.

If you want to know the number of users who have logged in for three days in a row, that is, the number of users who have logged in on May 17th, 18th and 19th.

The landing situation in the past three days is as follows:

On May 17, 3,8,23,32 users logged in.

Users logged in on May 18 on 3,23,43 and 54.

On May 19, users logged in on 3, 5, 23, 32, 56 and 78.

127.0.0.1 user:view:2020 6379 > bitop and three:and user:view:2020-5-17 user:view:2020-5-18 user:view:2020-5-19127.0.1 user:view:2020 6379 > bitcount three:and (integer) 2

If you want to know, how many users have logged in in the past three days.

127.0.0.1 user:view:2020 6379 > bitop or three:or user:view:2020-5-17 user:view:2020-5-18 user:view:2020-5-19 (integer) 10127.0.1 user:view:2020 6379 > bitcount three:or (integer) 9

As you can see, a total of 9 users have logged in in the past three days.

Actual combat

After talking about the knowledge above, we can complete the requirements we want: we need to count the users who have logged in continuously in a week, and the users who have logged in in a month.

First, simulate the login of users within 30 days. The pseudo code is as follows:

For ($I = 0; $I

< 20000; $i++) { $userId = mt_rand(1, 10000); $date = time() - 86400 * mt_rand(0, 30); $key = 'userlogin_'.date('Ymd', $date); $redis->

SetBit ($key, $userId, 1);}

To get users who have logged in within a week, of course, we will not take them all at once, but like paging, take a certain number of users at a time, and the pseudo code is as follows:

For ($iTun1; $I bitOp ('and',' week_logined', $key);} else {$redis- > bitOp ('and',' week_logined', 'week_logined', $key);}} / / get the first 50 users $userIds = []; for ($ibid1; $igetBit (' week_logined', $I); $ret & & $userIds [] = $I; if (count ($userIds) > = 50) break;}

There's a note here, and it's also a fallible point. In bitop, for the first time, because week_logined doesn't exist, there's only one key to op. When starting from the second time, there are two keys for op.

To get users who log in within one month, the idea is basically the same as above, except that the and is changed to or.

For ($iTun1; $I bitOp ('or',' month_loginOnce', 'month_loginOnce', $key);} / / get users logged in within one month $userIds = []; for ($ibid1; $igetBit (' month_loginOnce', $I); $ret & & $userIds [] = $I;}

As you can see, there are some differences between or and and. When or, there is no need to judge for the first time. The reason is for everyone to experience.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report