Redis 位图

Redis 位图简介

位图不是一种实际的数据类型,而是一组面向位的作 在 String 类型上定义,该类型被视为位向量。 由于字符串是二进制安全 blob,并且其最大长度为 512 MB,因此 它们适用于设置多达 2^32 个不同的钻头。

您可以对一个或多个字符串执行按位运算。 位图用例的一些示例包括:

  • 对于集合成员对应于整数 0-N 的情况,有效的集合表示形式。
  • Object 权限,其中每个位表示一个特定权限,类似于文件系统存储权限的方式。

基本命令

  • SETBIT将提供的偏移量处的位设置为 0 或 1。
  • GETBIT返回给定偏移量处的位值。

请参阅位图命令的完整列表

假设您有 1000 名骑自行车的人在乡村比赛,他们的自行车上的传感器标记为 0-999。 您希望快速确定给定传感器是否在一小时内 ping 了追踪服务器以检查乘客的情况。

您可以使用其键引用当前小时的位图来表示此方案。

  • Rider 123 于 2024 年 1 月 1 日 00:00 小时内对服务器执行 ping作。然后,您可以确认 Rider 123 已 ping 服务器。您还可以检查 rider 456 是否在同一小时内 ping 了服务器。

Bit Operations

Bit operations are divided into two groups: constant-time single bit operations, like setting a bit to 1 or 0, or getting its value, and operations on groups of bits, for example counting the number of set bits in a given range of bits (e.g., population counting).

One of the biggest advantages of bitmaps is that they often provide extreme space savings when storing information. For example in a system where different users are represented by incremental user IDs, it is possible to remember a single bit information (for example, knowing whether a user wants to receive a newsletter) of 4 billion users using just 512 MB of memory.

The SETBIT command takes as its first argument the bit number, and as its second argument the value to set the bit to, which is 1 or 0. The command automatically enlarges the string if the addressed bit is outside the current string length.

GETBIT just returns the value of the bit at the specified index. Out of range bits (addressing a bit that is outside the length of the string stored into the target key) are always considered to be zero.

There are three commands operating on group of bits:

  1. BITOP performs bit-wise operations between different strings. The provided operations are AND, OR, XOR and NOT.
  2. BITCOUNT performs population counting, reporting the number of bits set to 1.
  3. BITPOS finds the first bit having the specified value of 0 or 1.

Both BITPOS and BITCOUNT are able to operate with byte ranges of the string, instead of running for the whole length of the string. We can trivially see the number of bits that have been set in a bitmap.

For example imagine you want to know the longest streak of daily visits of your web site users. You start counting days starting from zero, that is the day you made your web site public, and set a bit with SETBIT every time the user visits the web site. As a bit index you simply take the current unix time, subtract the initial offset, and divide by the number of seconds in a day (normally, 3600*24).

This way for each user you have a small string containing the visit information for each day. With BITCOUNT it is possible to easily get the number of days a given user visited the web site, while with a few BITPOS calls, or simply fetching and analyzing the bitmap client-side, it is possible to easily compute the longest streak.

Bitmaps are trivial to split into multiple keys, for example for the sake of sharding the data set and because in general it is better to avoid working with huge keys. To split a bitmap across different keys instead of setting all the bits into a key, a trivial strategy is just to store M bits per key and obtain the key name with bit-number/M and the Nth bit to address inside the key with bit-number MOD M.

Performance

SETBIT and GETBIT are O(1). BITOP is O(n), where n is the length of the longest string in the comparison.

Learn more