Table of Contents
SETBIT" >SETBIT
GETBIT" >GETBIT
BITCOUNT" >BITCOUNT
BITPOS" > BITPOS
BITOP" >BITOP
应用场景" >应用场景
用户行为记录器
用户上线统计
Home Database Redis An in-depth analysis of bitmaps in Redis (bitmap)

An in-depth analysis of bitmaps in Redis (bitmap)

Dec 02, 2021 am 10:17 AM
redis bitmap

This article will take you through the bitmap in Redis, I hope it will be helpful to you!

An in-depth analysis of bitmaps in Redis (bitmap)

The bitmap of Redis is an array composed of multiple binary bits. Each binary bit in the array has a corresponding offset (from Starting from 0), these offsets can be used to operate on one or more binary bits specified in the bitmap. [Related recommendations: Redis Video Tutorial]

Actually, bitmap is not a new data type provided by Redis, it is an extension of the string type. Therefore, bitmap commands can be used directly on string type keys, and keys operated by bitmap commands can also be operated by string type commands.

For example, there is a string key foo:

redis> set foo bar
Copy after login

1 byte consists of 8 binary bits, so the binary form of the foo key is:

An in-depth analysis of bitmaps in Redis (bitmap)

SETBIT

Through the SETBIT command, you can specify the binary bit setting value at the offset for the bitmap, offset must be greater than or equal to 0 , 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 will return the old value before the binary bit is set as the result.

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

redis> setbit foo 6 0
(integer) 1
redis> setbit foo 7 1
(integer) 0
redis> get foo"aar"
Copy after login

When executing 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 will expand the set bitmap and initialize the values ​​of all unset binary bits to 0. For example:

redis> setbit far 10 1
Copy after login

Since far does not exist, Redis will set the binary bits from 0 to 9 to 0. Because Redis expands the bitmap in bytes, in fact far There are 16 binary bits in total, not 10, and the binary bits 11~15 are also 0.

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

GETBIT

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

GETBIT key offset

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

BITCOUNT

The BITCOUNT command can be used to count the number of binary bits with a value of 1 in the bitmap. 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. It can also be used to Select the start parameter and end parameter to let BITCOUNT only count the binary bits within the specified byte range (not the binary offset). For example, you want to count the number of binary numbers with a value of 1 in the two bytes of ar:

redis> bitcount foo 1 2
(integer) 7
Copy after login

The start and end parameters also support the use of negative indexes. The usage below is equivalent to the above:

redis> bitcount foo -2 -1
(integer) 7
Copy after login

BITPOS

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

BITPOS key value [start end]

BITPOS also accepts optional start parameters and end parameters, allowing the BITPOS command to only specify bytes Search in binary bits within the range.

redis> get foo"aar"redis> bitpos foo 1
(integer) 1
redis> bitpos foo 0
(integer) 0
redis> bitpos foo 0 1 2
(integer) 8
redis> bitpos foo 1 1 2
(integer) 9
redis> bitpos foo 1 -1 -1
(integer) 17
Copy after login

Handling for boundaries:

  • When trying to find a binary value of 1 in a bitmap that does not exist or a bitmap with all bits set to 0 bit, the BITPOS command will return -1 as a result.
  • If a binary bit with a value of 0 is found in a bitmap with all bits set to 1, the BITPOS command will return the maximum offset of the bitmap plus 1 as the result

BITOP

Use the BITOP command to perform specified binary bit operations on one or more bitmaps and store the operation results in the specified key. .

BITOP operation destkey key [key ...]

operation 参数的值可以是 AND、OR、XOR、NOT 中的任意一个,这 4 个值分别对应逻辑并、逻辑或、逻辑异或和逻辑非 4 种运算,其中 AND、OR、XOR 这 3 种运算允许用户使用任意数量的位图作为输入,而 NOT 运算只允许使用一个位图作为输入。BITOP 命令在将计算结果存储到指定键中之后,会返回被存储位图的字节长度。

当 BITOP 命令在对两个长度不同的位图执行运算时,会将长度较短的那个位图中不存在的二进制位的值看作 0。

redis> set foo1 bar
OK
redis> set foo2 aar
OK
redis> bitop or res foo1 foo2
(integer) 3
redis> get res"car"
Copy after login

An in-depth analysis of bitmaps in Redis (bitmap)

注意:BITOP 可能是一个缓慢的命令,它的时间复杂度是 O(N),在处理长字符串时应注意一下效率问题。

应用场景

用户行为记录器

用用户 ID 作为偏移量,若用户做了某种行为则通过 SETBIT 将二进制位设置为 1,通过 GETBIT 判断用户是否做了某种行为,通过 BITCOUNT 可以知道有多少用户执行了行为。

用户上线统计

可以使用 SETBIT 和 BITCOUNT 来实现,以用户 ID 作为 key ,假设今天是上线统计功能开放的第一天,ID 为 1 的用户上线后就通过 SETBIT 1 0 1。当要计算此用户的总共以来的上线次数时,使用 BITCOUNT 命令就可以得出的结果。

使用这种方式存储数据,即使 10 年后,1个用户就只占用几百字节的内存,它的处理速度依然很快。如果 bitmap 数据比较大,建议将 bitmap 拆分成多个小的 bitmap 分别进行处理。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of An in-depth analysis of bitmaps in Redis (bitmap). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles