[Redis] Introduction to Redis bitmap (bitmap) and its application in the sign-in scene

I. Introduction

Fundamental:

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

So the essential structure of the bitmap is the string type SDS (simple dynamic string) of redis.
insert image description here

Computer storage is all data stored in binary 0 and 1, and the bitmap is to directly read and assign values ​​to the bits of the data.

2. Redis bitmap related instructions

setbit command

  • Command: SETBIT key offset value
  • Complexity: O(1)
  • Meaning: Set or clear the bit value of the key's value (string) at offset (only 0 or 1). The offset is a negative number or a floating point number, and an error will be reported when the execution is executed. (set the original bit value stored at offset) getbit command

getbit command

  • Command: getbit key offset
  • Complexity: O(1)
  • Meaning: Get the bit value of the key's value (string) at offset (only 0 or 1). (get the bit value stored at offset.)

bitcount command

  • Command: bitcount key
  • Complexity: O(N)
  • Meaning: Get the total number of all bits set to 1 in the value (string) of the key (The number of bits set to 1).

bitfield command

  • Command: bitfield key [option1] [option2] ...
  • Complexity: O(1) * per specified subcommand
  • Meaning: Manipulates a multi-byte field, it will perform a series of operations, and return an array of responses, each response array matches the corresponding operation in the parameter list.
  • Official document: http://www.redis.cn/commands/bitfield.html

bitpos command

  • Command: bitpos key 0|1
  • Complexity: O(N)
  • Meaning: The command returns the first bit in the string that is set to 1 or 0. (The start and end parameters can also be connected later, but the unit of the offset is bytes, not bits, which is almost useless)

bitop command

  • 指令:BITOP [AND|OR|NOT|XOR ]destkey key1 key2
  • Complexity: O(N)
  • Meaning: Perform bit operations on one or more string keys that store binary bits, and store the result on destkey.
  • Official document: http://www.redis.cn/commands/bitop.html

Space occupancy & the time required for the first allocation of space
The space occupancy increases with the increase of the offset. In the redis4.0 version, you can use memory usage [key] to check the space occupancy of the key.
insert image description here

Official document data
offset is 2^32-1 (512MB allocated) needs ~300ms
offset is 2^30-1 (128MB allocated) needs ~80ms
offset is 2^28-1 (32MB allocated) needs ~30ms
offset is 2^26 -1 (allocating 8MB) requires 8ms and
the approximate space occupation calculation formula is: ($offset/8/1024/1024)MB

3. Application scenarios

  • Check-in is a typical bitmap usage scenario
  • Introduction to Command Application
  • getbit View check-in status
  • setbit flag check-in
  • bitcount Cumulative sign-in times
  • bitfield batch query check-in mark

The following is a demonstration of the sign-in application scenario. If there is still some logic for receiving user rewards after sign-in, and of course user behaviors such as re-signing may be involved, two bits can be used to implement the logic.

Cumulative sign-in application scenarios

insert image description here
insert image description here

Continuous sign-in application scenarios

insert image description here
insert image description here

Date check-in scene

insert image description here

insert image description here

Application Details

Getbit checks the value of the nth bit----"Nth bit" identifies
the screenshot of the command example of "the check-in status of the day":
insert image description here

Meaning:
check that the key is the value of Test4SignMzy, the first bit is 0
Need to use:
the key can be used as a certain cycle of the sign-in activity, and the "Nth bit" identifies the "sign-in status of the day".
setbit marks the nth bit as 0 or 1----the user "checks in on the nth day" and marks the "nth bit" as 1
command example screenshot:
insert image description here

meaning:

将key为 Test4SignMzy 的value值,第一位比特位赋值为1,然后查看到比特位以改变为1。

Required application:
The key can be used as a certain period of the sign-in activity, and the user "signs in on the nth day" will mark the "nth bit" of the value corresponding to the redis key as 1.
bitcount counts the number of 1s in all bits----users use this value to query "accumulative sign-in N times"
command example screenshot:

meaning:

key为 Test4SignMzy 的value值,所占用的所有比特位中,比特位值为1的总数是4个。

Required application:
The key can be used as a certain period of the sign-in activity. If the user "signs in N times accumulatively", you can use the value to know how many bits are 1 in total.
The bitfield command can operate on multiple bit ranges at the same time in one call.
Screenshot of the command example:
insert image description here

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对应的value的第1、2、3、4、8、9、10、99比特位的值,得到的值当作无符号1位整数返回。加入原本不存在第99位,返回0代替。 

第二个执行“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”中第三个参数“N”表示从这个key的value值的第0位开始,往后偏移N位比特位;而“get u1 #N”中第三个参数“#N”表示从这个key的value值的第0位开始,往后偏移“N乘1(第二个参数表示的长度)”位比特位。 

Demand application:
You can check the sign-in status of a user for several days in a row, or all days in a cycle, so that you can get how many days of continuous sign-in at present and the maximum number of days of continuous sign-in in a cycle.

Guess you like

Origin blog.csdn.net/u011397981/article/details/130691026