Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is bitmap in Redis

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly talks about "what is bitmap in Redis". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is bitmap in Redis"?

The bitmap (bitmap) of a Redis is an array of multiple binary bits, each of which has an offset corresponding to it (starting at 0). One or more binary bits specified in the bitmap can be manipulated by these offsets.

In fact, bitmaps are not a new data type provided by Redis, it is an extension of the string type. So the bitmap command can be directly used on the string type key, and the bitmap command operation key can also be operated by the string type command.

For example, there is an existing string key foo:

Redis > set foo bar

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

SETBIT

With the SETBIT command, you can specify the binary setting value on the offset for the bitmap. The offset must be greater than or equal to 0. the value can only be 0 or 1. The time complexity of this command is O (1).

SETBIT key offset value

After setting the binary bit, the SETBIT command returns the old value before the binary bit was set as a result.

Suppose you want to change bar to aar now, you only need to do the following two steps:

Redis > setbit foo 6 0 (integer) 1redis > setbit foo 7 1 (integer) 0redis > get foo "aar"

When you execute the SETBIT command to try to set a bitmap, if the bitmap does not exist or the current size of the bitmap cannot be satisfied, Redis extends the set bitmap and initializes the value of all unset binary bits to 0. For example:

Redis > setbit far 10 1

Because far does not exist, Redis sets the binary bit of 0x9 to 0, because Redis expands the bitmap in bytes, so there are actually 16 binary bits in far, not 10, and the binary bit of far 15 is also 0.

Based on this situation, when the specified binary offset is too large, Redis needs to allocate all memory at once, which may cause the Redis server to block. For example, the gender of the user is stored. 1 represents male, 0 represents female, and ID is used as the binary offset. If the ID starts from 10000000001, you need to subtract 10000000000 from the user ID before storing, otherwise memory will be wasted.

GETBIT

Use the GETBIT command to get the value of the binary bit on the specified offset of the bitmap. The time complexity of this command is O (1).

GETBIT key offset

If the offset entered exceeds the maximum offset currently owned by the bitmap, 0 is returned as the result.

BITCOUNT

You can count the number of binary bits with a value of 1 in the bitmap through the BITCOUNT command. The time complexity of this command is O (n).

BITCOUNT key [start end]

By default, the BITCOUNT command counts the binary bits in all bytes contained in the bitmap, or you can have BITCOUNT count only the binary bits within a specified range of bytes (not the binary offset) with the optional start and end parameters. For example, you want to count the number of binaries with a value of 1 in two bytes of ar:

Redis > bitcount foo 1 2 (integer) 7

The start and end parameters also support negative indexing, and the usage below is equivalent to the one above:

Redis > bitcount foo-2-1 (integer) 7BITPOS

Find the first binary bit set to the specified value in the bitmap by executing the BITPOS command, and return the offset of this binary bit.

BITPOS key value [start end]

BITPOS also accepts the optional start and end parameters, allowing the BITPOS command to look only in binary bits within a specified range of bytes.

Redis > get foo "aar" redis > bitpos foo 1 (integer) 1redis > bitpos foo 0 (integer) 0redis > bitpos foo 0 12 (integer) 8redis > bitpos foo 1 12 (integer) 9redis > bitpos foo 1-1-1 (integer) 17

Handling of boundaries:

When trying to find a binary bit with a value of 1 in a bitmap that does not exist or where all bits are set to 0, the BITPOS command returns-1 as a result.

If you look for a binary bit with a value of 0 in a bitmap where all bits are set to 1, the BITPOS command returns the bitmap maximum offset plus 1 as a result

BITOP

The specified binary bit operation is performed on one or more bitmaps through the BITOP command, and the result of the operation is stored in the specified key.

BITOP operation destkey key [key...]

The value of operation parameter can be any one of AND, OR, XOR, NOT. These four values correspond to logical union, logical OR, logical XOR and logical non-operation, respectively. AND, OR and XOR operations allow users to use any number of bitmaps as input, while NOT operations allow only one bitmap as input. The BITOP command returns the byte length of the stored bitmap after storing the result of the calculation in the specified key.

When the BITOP command performs an operation on two bitmaps of different lengths, it treats the value of the binary bits that do not exist in the shorter bitmap as 0.

Redis > set foo1 barOKredis > set foo2 aarOKredis > bitop or res foo1 foo2 (integer) 3redis > get res "car"

Note: BITOP may be a slow command with a time complexity of O (N), so you should pay attention to efficiency when dealing with long strings.

Application scene user behavior Recorder

The user ID is used as the offset, and if the user does a certain behavior, the binary bit is set to 1 through SETBIT, and whether the user has done a certain behavior is judged by GETBIT. Through BITCOUNT, we can know how many users have performed the behavior.

User online statistics

You can use SETBIT and BITCOUNT to implement it, using the user ID as the key. Suppose today is the first day when the online statistics feature is open. Users with ID 1 will go through SETBIT 101 after they go online. The result you can get by using the BITCOUNT command when you want to calculate the total number of times this user has been online.

Using this method to store data, even if a user takes up only a few hundred bytes of memory 10 years later, its processing speed is still very fast. If the bitmap data is large, it is recommended to split the bitmap into multiple small bitmap for processing.

At this point, I believe you have a deeper understanding of "what is bitmap in Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 204

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report