First praise after look, form a habit ❤️

What is the BitMap

  • Eight bits make up a Byte, so a bitmap saves a lot of storage and you can think of it as a specially processed string
  • Key indicates service attributes and labels. A bit that represents the value or state of an element.

    For example: register daily active users, key representsThe login time, 1, 2, 3… Represent user ID
key 0 1 2 3 4 5 6 7
login20191230 0 0 1 0 0 0 0 0
login20191231 0 1 0 0 1 0 0 0
login20200101 0 0 1 0 0 0 0 0

According to the bitmap, user ID: 1 has logged in to the system in 20191231. User ID: 2 has logged in to the system in 20191230 and 20200101. User ID: 4 has logged in to the system in 20191231

The BitMap in Redis

Is already “new technology” from version 2.2.0. I think you’ll see that the double quotes don’t really add setbit,getbit,bitcount, and other bitmap-related commands. But commands like setbit are just extensions of set.

Introduction to the setbit command

SETBIT key offset value complexity O(1) Sets or clears the key’s value(string) bit value at offset (only 0 or 1).

Space usage, and how long it takes to allocate space for the first time

On a 2010MacBook Pro

  • The offset is 2^32-1 (512MB is allocated). The value ranges from 300ms
  • The offset is 2^30-1(allocated 128MB). The value ranges from 80ms
  • The offset is 2^28-1 (32MB). The value ranges from 30ms
  • Offset is 2^26-1 (8MB is allocated) 8ms is required. — < from official documentation >

    The approximate calculation formula for space occupancy is:($offset/8/1024/1024)MB



Usage scenarios

Counting active users

Use time as the cacheKey and set the user ID to offset, set to 1 if the user was active on that day. How do I count active users for a certain day/month/year? BITOP operation destkey [key…] Note: Perform bitwise operations on one or more string keys that store binary bits, and save the result to destkey. Note: The BITOP command supports any of the four operations: AND, OR, NOT, AND XOR

// Date corresponds to the active user
$data = array(
    '2020-01-10'= >array(1.2.3.4.5.6.7.8.9.10),
    '2020-01-11'= >array(1.2.3.4.5.6.7.8),
    '2020-01-12'= >array(1.2.3.4.5.6),
    '2020-01-13'= >array(1.2.3.4),
    '2020-01-14'= >array(1.2));// Set the active state in batches
foreach($data as $date=>$uids) {
    $cacheKey = sprintf("stat_%s", $date);
    foreach($uids as $uid) {
        $redis->setBit($cacheKey, $uid, 1);
    }
}

$redis->bitOp('AND'.'stat'.'stat_2020-01-10'.'stat_2020-01-11'.'stat_2020-01-12');

// Total active users: 6
echo "Total active Users:" . $redis->bitCount('stat') . PHP_EOL;

$redis->bitOp('AND'.'stat1'.'stat_2020-01-10'.'stat_2020-01-11'.'stat_2020-01-14') . PHP_EOL;

// Total active users: 2
echo "Total active Users:" . $redis->bitCount('stat1') . PHP_EOL;

$redis->bitOp('AND'.'stat2'.'stat_2020-01-10'.'stat_2020-01-11') . PHP_EOL;

// Total active users: 8
echo "Total active Users:" . $redis->bitCount('stat2') . PHP_EOL;
Copy the code

Assuming the current site has 5000W users, the data in a day is approximately 5000000/8/1024/1024 =6MB

User check-in and online status

It’s all the same and I’m not gonna slow murder your time. Peace&Love

What should I pay attention to

You need to compress

I think people’s cognition of things, after ignorant but beautiful longing -> cheated (cheated) feelings -> look at yourself and things again to establish an objective and correct cognition – more appreciation, more appreciation

The great thing about Redis Bitmap is that ta compresses storage space. In everyday usage, the cost of this compression is computed by the CPU.

SetBit with a lot of data causes a lot of network requests. So the program usually sets the carry map of the ID array Pack () to a String. Set into Redis once again.

This means that unpack() unpacks the String into an array of ids when it comes out. But thanks to the algorithm, it’s not too complicated

Limited data storage

In addition, ta stores very limited data, for example:

// Normal user id: 1, 3;
'login20191230'= >array(
    1= >array(
        'user_id'= >1.'login_ip'= >'x.x.x.x'.'usage_agent'= >'xxx'
    ),
    3= >array(
        'user_id'= >3.'login_ip'= >'x.x.x.x'.'usage_agent'= >'xxx'),// An array of bitmap logins
'login20191230'= >array(
    0= >0.1= >1.2= >0.3= >1.)Copy the code

The only things you can use are ‘login20191230’ and the key in the array

You will ask the array value is not look can also change it I give you an expression to understand their own 😑. It is ok to do STH over and over again, but it is not balanced