This is my second article on getting started
In Redis’ official definition, bitmaps is not a separate data type, but a binary representation of a string, a subset of the string. We know that 8bit=1 byte, and that bitmaps represent the value of a location in bits, thus greatly reducing the storage space. Although bitmaps are not often used in everyday use, as a mid-level developer, you should still be familiar with the features and common uses of bitmaps.
Bitmaps basic features
- If through
type
Command to query the key. The value is a string - String by string
get key
“Is the ASCII value of bitmaps - The default value is 8 bits (8 bits =1byte). If the value exceeds 8 bits, 8 bits will be added. The maximum number of bits is 2^32
- The value can only be 0 or 1; passing any other value will cause an error
Bitmaps common command
setbit key offset value
New data instruction. The first argument is the bit number, and the second argument is the value of the bit to be set, which is either 1 or 0. If the address bit exceeds the length of the current string, the command automatically enlarges the corresponding string length
getbit key offset
Example Query the corresponding index value. An out-of-range bit (addressing a bit outside the length of the string stored in the target key) returns 0.
bitcount key [start] [end]
Count how many 1’s there are in the corresponding key
bitop op destkey key [key ...]
The bitop command intersects, or, not, or xOR multiple bitmaps and stores the results in the destKey.
Common Usage Scenarios
Because of the spatial nature of Bitmaps, it’s more likely to be used in tens of millions of data processes. Especially in the sign – in, statistics DAU,MAU
1. Count how many people landed today (2021.07.08)
bitcount juejin:20210708
Copy the code
2. Count the number of login users in 3 days
bitop or threeday:or juejin:20210706 juejin:20210707 juejin:20210708
bitcount threeday:or
Copy the code
3. Count the number of users in the last three dayscontinuousThe login
bitop and threeday:and juejin:20210706 juejin:20210707 juejin:20210708
bitcount threeday:and
Copy the code
In fact, BitMap can be used in many scenarios (of course, there are some limitations), thinking can continue to spread ~