Redis Bitmaps bitmap

Redis Bitmaps is not an actual data type, but a set of bit-oriented operations defined on the String type.

Because String is binary safe, with a maximum length of 512MB, it is suitable for constructing 2 of 32 different bits, and storing 0 or 1 on each bit is suitable for storing boolean type information.

This method of recording, compared to the use of key/value, the biggest advantage is that it can save a lot of storage space.

 

1. Single bit assignment and query extraction operations, setbit key bit_number bit_value, getbit key,

Among them, the first parameter of the setbit command is the key, the second parameter is the number of bits, counting from 0, and the third parameter is the value stored in the bit, the value is 1 or 0, and the default is 0 if it is not written.

For example: setbit bitKey 100 1, setbit bitKey 10 1, setbit bitKey 0 1

getbit bitKey 10, returns 1

 

2. Operation based on a set of bits

bitcount bitKey, count the number of bits stored in 1.

bitpos bitKey 1, the first bit position is 1

bitpos bitKey 0, the first bit position is 0

This bitcount statistical function records whether website users log in every day during a certain period of time. If they log in, it will record 1, and if they do not log in, it will record 0.

setbit user:123 1 1

setbit user:123 3 1

setbit user:123 10 1

setbit user:123 12 1

setbit user:123 1 51

Then use bitcount user:123 to count the actual number of days logged in.

 

3. Bitop bit operation

How to use: bitop operation resultKey bitKey1 bitKey2 bitKey3

It means to perform operation on the data of bitKey1, bitKey2, bitKey3, and the result is stored in resultKey

operation can be and logical union, or logical OR, xor logical exclusive OR, not not

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/suoyx/article/details/114735856