Home >Backend Development >PHP Tutorial >PHP+REDIS practice: analysis of several solutions for counting online people

PHP+REDIS practice: analysis of several solutions for counting online people

藏色散人
藏色散人forward
2021-03-17 17:24:263840browse

Online people counting business is the business logic that we must design when developing web. This article will give several design solutions to analyze the advantages and disadvantages of each solution:

  • Use Ordered set

This solution can store online users and user online time at the same time, and can perform a lot of aggregation calculations, but the memory consumed is also very considerable.

  • Use sets

This solution can store online users and can also perform certain aggregation calculations. Compared with ordered sets, it consumes less The memory is smaller, but as the number of users increases, the memory space consumed is also increasing

  • Use hyperloglog

This solution No matter how many online users are counted, the memory consumed is 12k, but only the statistical information of online users can be given, and the accurate list of online users cannot be obtained

  • Use bitmap

This solution is still relatively good. It records the status of online users while saving memory space as much as possible, and can perform certain aggregation operations.

Let’s use practical examples below. Note:

# 说 We first have a small number of users of 10 ~ 30W every day, and the user group of 100W will explain the following schemes

## 说 说 说 一 一 一 一 一 一 一 说 一 说 一 说 一 说 一 说 一 说 一 说 一 说 说 说 一 说 一 说 说 说 一 说 说 说 说 说 一 说 一 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 一 说 说 说 说 说 说 说 说 说 I. :Use an ordered set

First generate user online record data:

$start_time = mktime(0, 0, 0, 9, 5);    //monday
for ($i=0; $i < 6; $i++) {
    $day_start_time  = $start_time + 86400 * $i;    //every day begin time
    $day_end_time =  $day_start_time + 86400;       //every day end time
    $online_user_num = mt_rand(100000, 300000);     //online user between 100000 and 300000 
    for ($j=1; $j < $online_user_num; $j++) { 
        $user_id = mt_rand(1, 1000000);
        $redis->zadd(&#39;000|online_users_day_&#39;.$i, mt_rand($day_start_time, $day_end_time), $user_id);
    }
}

Okay, write it down and let’s see what information we can count

//note: 统计每天的在线总人数
for ($i=0; $i < 6; $i++) { 
    print_r($redis->zsize(&#39;000|online_users_day_&#39;.$i). "\n");
}
//note: 统计最近6天都在线的人数
var_dump($redis->zInter(&#39;000|online_users_day_both_6&#39;, 
    [
    &#39;000|online_users_day_0&#39;, 
    &#39;000|online_users_day_1&#39;, 
    &#39;000|online_users_day_2&#39;, 
    &#39;000|online_users_day_3&#39;, 
    &#39;000|online_users_day_4&#39;, 
    &#39;000|online_users_day_5&#39;
    ]
    ));
//note: 统计出近6天中共有多少上线
$redis->zunion(&#39;000|online_users_day_total_6&#39;, [&#39;000|online_users_day_0&#39;, &#39;000|online_users_day_1&#39;, &#39;000|online_users_day_2&#39;, &#39;000|online_users_day_3&#39;, &#39;000|online_users_day_4&#39;, &#39;000|online_users_day_5&#39;]);
//note: 统计某个时间段总共在线用户
print_r($redis->zcount(&#39;000|online_users_day_5&#39;, mktime(13, 0, 0, 9, 10), mktime(14, 0, 0, 9, 10)));
//note: 统计某个时间段在线用户名单
print_r($redis->zrangebyscore(&#39;000|online_users_day_5&#39;, mktime(13, 0, 0, 9, 10), mktime(14, 0, 0, 9, 10), 
    array(&#39;withscores&#39; => TRUE)));

Not only these, we can also count the online status of users in the morning, noon, afternoon, evening and other time periods, and there are many others. Let us use our imagination. Isn’t there a lot? It’s just that it does consume a lot of memory space

[Recommended:

PHP video tutorial]

Option 2: Use collections

or First, let’s record data online for users:

//note set 一般聚合
for ($i=0; $i < 6; $i++) {
    $online_user_num = mt_rand(100000, 300000);     //online user between 100000 and 300000 
    for ($j=1; $j < $online_user_num; $j++) { 
        $user_id = mt_rand(1, 1000000);
        $redis->sadd(&#39;001|online_users_day_&#39;.$i, $user_id);
    }
}

Okay, write it down and let’s see what information we can get.

//note 判断某个用户是否在线
var_dump($redis->sIsMember(&#39;001|online_users_day_5&#39;, 100030));
//note 每天在线用户总量的统计
for ($i=0; $i < 6; $i++) { 
    print_r($redis->ssize(&#39;001|online_users_day_&#39;.$i). "\n");
}
//note 对不同时间段的在线用户名单进行聚合
print_r($redis->sInterStore(&#39;001|online_users_day_both_4and5&#39;, &#39;001|online_users_day_4&#39;, &#39;001|online_users_day_5&#39;). "\n");
//note 对指定的时间段的在线用户名单进行统计
print_r($redis->sUnionStore(&#39;001|online_users_day_total_4add5&#39;, &#39;001|online_users_day_4&#39;, &#39;001|online_users_day_5&#39;). "\n");
//note 哪天上线哪天没上线
print_r($redis->sDiffStore(&#39;001|online_users_day_diff_4jian5&#39;, &#39;001|online_users_day_4&#39;, &#39;001|online_users_day_5&#39;). "\n");

Isn’t it pretty good? Don’t worry first. Let’s move on to

Option 3: Use hyperloglgo

First come to the user to record data online:

// note HyperLogLog 只需要知道在线总人数
for ($i=0; $i < 6; $i++) {
    $online_user_num = mt_rand(100000, 300000);     //online user between 100000 and 300000 
    var_dump($online_user_num);
    for ($j=1; $j < $online_user_num; $j++) { 
        $user_id = mt_rand(1, 1000000);
        $redis->pfadd(&#39;002|online_users_day_&#39;.$i, [$user_id]);
    }
}

Let’s take a look at this solution Let’s see what services can be achieved.

$count = 0;
for ($i=0; $i < 3; $i++) { 
    $count += $redis->pfcount(&#39;002|online_users_day_&#39;.$i);
    print_r($redis->pfcount(&#39;002|online_users_day_&#39;.$i). "\n");
}
var_dump($count);
//note  3 days total online num
var_dump($redis->pfmerge(&#39;002|online_users_day_both_3&#39;, [&#39;002|online_users_day_0&#39;, &#39;002|online_users_day_1&#39;, &#39;002|online_users_day_2&#39;]));
var_dump($redis->pfcount(&#39;002|online_users_day_both_3&#39;));

There are so few. Yes, this solution can only count the total number of online people in a certain period of time, but it can’t do anything about the list of online users, but it is quite good. We can consider this solution if it saves memory and does not require much statistical data.

Option 4: Use bitmap

The author actually likes this solution. It consumes not much memory space but has a lot of statistical information. It is still old. Steps, first generate data:

//note bitmap 综合前面3个的优缺点
for ($i=0; $i < 6; $i++) {
    $online_user_num = mt_rand(100000, 300000);     //online user between 100000 and 300000 
    for ($j=1; $j < $online_user_num; $j++) { 
        $user_id = mt_rand(1, 1000000);
        $redis->setbit(&#39;003|online_users_day_&#39;.$i, $user_id, 1);
    }
}

Next, let’s take a look at the statistical information that can satisfy

//note userid today whether online 
var_dump($userid = mt_rand(1, 1000000));
var_dump($redis->getbit(&#39;003|online_users_day_5&#39;, $userid));
//note how many user is online
var_dump($redis->bitcount(&#39;003|online_users_day_5&#39;));
//note 6 days both online
var_dump($redis->bitop(&#39;AND&#39;, &#39;003|online_users_day_both_6&#39;, &#39;003|online_users_day_0&#39;, &#39;003|online_users_day_1&#39;, &#39;003|online_users_day_2&#39;, &#39;003|online_users_day_3&#39;, &#39;003|online_users_day_4&#39;, &#39;003|online_users_day_5&#39;));
var_dump($redis->bitcount(&#39;003|online_users_day_both_6&#39;));
//note 6 days total online
var_dump($redis->bitop(&#39;OR&#39;, &#39;003|online_users_day_total_6&#39;, &#39;003|online_users_day_0&#39;, &#39;003|online_users_day_1&#39;, &#39;003|online_users_day_2&#39;, &#39;003|online_users_day_3&#39;, &#39;003|online_users_day_4&#39;, &#39;003|online_users_day_5&#39;));
var_dump($redis->bitcount(&#39;003|online_users_day_total_6&#39;));
//note 6 days only one online
var_dump($redis->bitop(&#39;XOR&#39;, &#39;003|online_users_day_only_one_6&#39;, &#39;003|online_users_day_0&#39;, &#39;003|online_users_day_1&#39;, &#39;003|online_users_day_2&#39;, &#39;003|online_users_day_3&#39;, &#39;003|online_users_day_4&#39;, &#39;003|online_users_day_5&#39;));
var_dump($redis->bitcount(&#39;003|online_users_day_only_one_6&#39;));

How about? If the collection can be counted, can this guy also count it? And it consumes less content.

In fact, each of these solutions has its own benefits. According to the business statistics, choose the corresponding solution to implement, so that the memory utilization will be more reasonable

The above is the detailed content of PHP+REDIS practice: analysis of several solutions for counting online people. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:饼bing. If there is any infringement, please contact admin@php.cn delete