This article will take you through the bitmap in Redis, I hope it will be helpful to you!
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
1 byte consists of 8 binary bits, so the binary form of the foo key is:
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"
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
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
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
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
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"
注意: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!

Redis's core functions include memory storage and persistence mechanisms. 1) Memory storage provides extremely fast read and write speeds, suitable for high-performance applications. 2) Persistence ensures that data is not lost through RDB and AOF, and the choice is based on application needs.

Redis'sServer-SideOperationsofferFunctionsandTriggersforexecutingcomplexoperationsontheserver.1)FunctionsallowcustomoperationsinLua,JavaScript,orRedis'sscriptinglanguage,enhancingscalabilityandmaintenance.2)Triggersenableautomaticfunctionexecutionone

Redisisbothadatabaseandaserver.1)Asadatabase,itusesin-memorystorageforfastaccess,idealforreal-timeapplicationsandcaching.2)Asaserver,itsupportspub/submessagingandLuascriptingforreal-timecommunicationandserver-sideoperations.

Redis is a NoSQL database that provides high performance and flexibility. 1) Store data through key-value pairs, suitable for processing large-scale data and high concurrency. 2) Memory storage and single-threaded models ensure fast read and write and atomicity. 3) Use RDB and AOF mechanisms to persist data, supporting high availability and scale-out.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

The main difference between Redis and SQL databases is that Redis is an in-memory database, suitable for high performance and flexibility requirements; SQL database is a relational database, suitable for complex queries and data consistency requirements. Specifically, 1) Redis provides high-speed data access and caching services, supports multiple data types, suitable for caching and real-time data processing; 2) SQL database manages data through a table structure, supports complex queries and transaction processing, and is suitable for scenarios such as e-commerce and financial systems that require data consistency.

Redisactsasbothadatastoreandaservice.1)Asadatastore,itusesin-memorystorageforfastoperations,supportingvariousdatastructureslikekey-valuepairsandsortedsets.2)Asaservice,itprovidesfunctionalitieslikepub/submessagingandLuascriptingforcomplexoperationsan

Compared with other databases, Redis has the following unique advantages: 1) extremely fast speed, and read and write operations are usually at the microsecond level; 2) supports rich data structures and operations; 3) flexible usage scenarios such as caches, counters and publish subscriptions. When choosing Redis or other databases, it depends on the specific needs and scenarios. Redis performs well in high-performance and low-latency applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
