This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

Basic Principles:

The implementation description of bitmap in Redis Design and Implementation is: Redis uses string object to represent the bit array, because the SDS data structure used by string object is binary safe, so the program can directly use SDS structure to save the bit array, and use the operation function of SDS structure to process as an array.

So bitmap structure is redis string type SDS(Simple Dynamic String) in essence.

The storage of computers is all data stored in binary 0 and 1. Bitmap is the operation of reading and assigning value to the bits of data directly.

Redis bitmap related instructions

Setbit command

SETBIT key offset value Complexity: O(1) Description: Set or clear the key value(string) at the offset bit (only 0 or 1). Offset indicates a negative number or floating point number. An error occurs during execution. (Set the original bit value stored at offset) Getbit commandCopy the code

Getbit command

Instruction: getbit key offset Complexity: O(1) Description: Obtain the key value(string) bit value at offset (only 0 or 1). (get the bit value stored at offset.)Copy the code

Bitcount command

Instruction: bitcount Key complexity: O(N) Meaning: Obtain The key value(string) The number of bits set to 1.Copy the code

Bitfield command

Instruction: bitfield key [option1] [option2].... Complexity: O(1) * Description of each specified subcommand: operation multi-byte field, which performs a series of operations and returns an array of responses, each of which matches the corresponding operation in the parameter list. The official document: http://www.redis.cn/commands/bitfield.htmlCopy the code

Bitpos command

Instructions: bitpos key 0 | 1 complexity: O (N) meaning: command returns a string inside the first bit is set to 1 or 0. (Start end can also be used, but the offset is in bytes, not bits, which is almost useless.)Copy the code

Bitop command

Instructions: BITOP [AND | OR | NOT | XOR] destkey key1 key2 complexity: O (N) : binary string of one OR more key bit operations, AND save the results to the destkey. The official document: http://www.redis.cn/commands/bitop.htmlCopy the code

Memory Usage & The memory usage required by the first space allocation increases as the offset increases. In Redis4.0, you can use memory Usage [key] to check the space usage of the key.

Official document data offset: 2^32-1 (512MB allocated) : 300ms offset: 2^30-1(128MB allocated) : 80ms offset: 2^28-1 (32MB allocated) : 30ms Offset is 2^26-1 (8MB is allocated) 8ms is required

$offset/8/1024/1024)MB

Application scenarios

Sign-in is a typical bitmap application scenario instruction Application Introduction getBit Check sign-in status setbit sign-in tag BitCount Total sign-in count bitfield Batch query sign-in tag

The following is a demonstration of the check-in application scenario. If there is still some logic for the user to receive rewards after the check-in, of course, it may also involve user behaviors such as re-signing, then two bits can be used to achieve the logic.

Application scenario of accumulative check-in

Continuous check-in application scenario

Date check-in scenario

Application details

Getbit View the NTH bit value —- “NTH bit” indicates the “Day of check-in status” command Example screenshot:



Description: Check the value of key Test4SignMzy where the first bit is 0

Demand application:

Key can be used as a period of the check-in activity, and the “NTH bit” identifies the “day of the check-in status”.

Setbit marks the NTH bit as 0 or 1—- the user marks the “NTH bit” as 1 on the “NTH day check-in”

Meaning:

Set the value of key to Test4SignMzy, assign the first bit to 1, and then look at the bits to change to 1.Copy the code

Demand application:

Key can be used as a certain period of check-in activities. When the user “checks in on the NTH day”, the “NTH bit” of the value corresponding to the redis key is marked as 1.

Bitcount Counts the number of bits that are 1 —- User number of sign-in times. Example Screenshot:



Meaning:

Key is the value of Test4SignMzy. Among all the bits used, the total number of bits with the value of 1 is 4.Copy the code

Requirement application: Key can be used as a certain period of check-in activities. If the user “accumulates N check-in times”, it can be known by the number of bits 1 in value.

The bitfield command can operate on multiple bit ranges simultaneously in a single call.

Meaning:

Bitfield Test4SignMzy get U1 1 get U1 2 get U1 3 get U1 4 get U1 8 get U1 9 get U1 10 get U1 99 Test4SignMzy = Test4SignMzy = Test4SignMzy = Test4SignMzy = Test4SignMzy = Test4SignMzy Add 99th bit that does not exist, return 0 instead. Bitfield Test4SignMzy get U1 #1 get U1 #2 get U1 #3 get U1 #4 get U1 #8 get U1 #9 get U1 #10 get U1 #99 Get u1 N = 0; get u1 N = 0; get u1 N = 0; Get u1 #N the third parameter in get u1 #N indicates that the value of the key starts at the 0th bit and is offset by N by 1(the length of the second parameter) bits.Copy the code

Demand application:

You can view the check-in status of a user for several consecutive days or all the days in a period to obtain the current number of consecutive check-in days and the maximum number of consecutive check-in days in a period.