Introduction to the

For some scenarios, such as counting the number of users’ check-ins in a year, using 0 and 1 to mark whether they check in or not, and recording 365 days, data structures such as key and value are usually used. When there are a large number of users, the storage space is also large.

The graph above shows how many times a user has checked in to the site in a 10-day period, with 1 representing check-in and 0 representing uncheck-in, which makes it easy to measure the user’s activity level. Instead of using strings directly, each record in a bitmap takes up only one bit, thus greatly reducing memory space usage.

Redis also ran an experiment where they simulated a system with 128 million users and then used Redis bitmaps to count the “daily user count”, which took about 50ms and used only 16 MB of memory.

The basic principle of

The advantages of bitmap manipulation, compared to strings, are not only efficient, but also very space-saving.

In fact, bitmaps are not a new data type provided by Redis, but an extension of the string type. So bitmap commands can be used directly on string keys, and keys operated by bitmap commands can be operated by string commands. For example, we have a string key foo:

redis> set foo bar
Copy the code

A byte consists of eight bits, so the binary form of the foo key is:

Common Commands

1. Basic commands SETBIT and GETBIT

SETBIT specifies the value of the binary bit on the bitmap’s offset. Offset must be greater than or equal to 0, and value must be 0 or 1. Use the GETBIT command to get the value of the bits at the bitmap’s specified offset. The time complexity of this command is O(1).

LingCun has the

127.0.0.1:6379> setbit s 1 1 (integer) 0 127.0.0.1:6379> getbit w 1 # Obtain the value of a specific position 0/1 (integer) 1Copy the code

Term has the

127.0.0.1:6379> set w h # total storage (integer) 0 127.0.0.1:6379> getbit W 1 (integer) 1Copy the code
2. Count and find BITCOUNT and BITPOS

You can use the BITCOUNT command to count the number of bits in the bitmap whose value is 1. The time complexity of this command is O(n). By executing the BITPOS command, the bitmap looks for the first bit set to the specified value and returns the offset of this bit.

127.0.0.1:6379> set w hello OK 127.0.0.1:6379> bitcount w (integer) 21 127.0.0.1:6379> bitcount w 0 0 # The number of bits of 1 in the first character (integer) 3 127.0.0.1:6379> bitcount w 0 1 # The number of bits of 1 in the first two characters (integer) 7 127.0.0.1:6379> bitpos w 0 # The first 0 bit (integer) 0 127.0.0.1:6379> bitpos w 1 # The first 1 bit (integer) 1 127.0.0.1:6379> bitpos w 1 1 1 1 # The first 1 bit (integer) 9 127.0.0.1:6379> bitpos w 12 2 # From the third character, the first 1 bit (integer) 17Copy the code
3. Magic instruction BITFIEID

BITFIEID has three sub-instructions, namely get/set/incrby, which can read and write the specified bit fragment, but can only handle up to 64 consecutive bits. If more than 64 bits are used, multiple sub-instructions will be used. BITFIEID can execute multiple sub-instructions at one time.

127.0.0.1:6379> set w hello OK 127.0.0.1:6379> bitfield w get u4 0 # The result is the unsigned number (u) (integer) 6 127.0.0.1:6379> bitfield w get u3 2 # start with 3 bits, The result is the unsigned number (u) (integer) 5 127.0.0.1:6379> bitfield w get i4 0 # The result is signed number (I) 1) (integer) 6 127.0.0.1:6379> bitfield w get i3 2 # Take 3 bits from the third bit, the result is signed number (I) 1) (integer) -3Copy the code

Negative numbers and binary conversion methods.

  • Remove the sign bit and subtract 1
  • Remove sign bit, reverse bit; The result is negative source code;
  • Convert the source code to the corresponding decimal

Usage scenarios

User behavior records, simple statistics class