Redis Bitmaps

2019-12-09  本文已影响0人  nzdxwl

Redis位图:
位图并不是一种实际的数据类型,而是在字符串数据类型上定义的按位操作集合(对普通的字符串键也可以进行位图操作)。由于字符数据是二进制安全的数据块并且最大长度可以达到512MB,它们适用于2的32次方个不同的位。

位操作可以分成两种:一种是常量时间单个位操作,例如单个位赋值或取值,一种是对多个位组成的位组进行操作,例如计算某个范围中有赋值位的数量等。

位图最大的优势之一就是使用它存储信息时可极大节约存储空间。

Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type. Since strings are binary safe blobs and their maximum length is 512 MB, they are suitable to set up to 232 different bits.

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 of users using just 512 MB of memory.

Bits are set and retrieved using the SETBIT and GETBIT commands

使用bitpos key bit_value(0或者1)来获取最近的0/1值位的位置
使用bitcount key [start end] 来获取指定的键中位值=1的个数,或者指定键的位范围中
还有bitop operation dest_key key [key1 .. ] 使用指定键值对目标键值按位操作,支持与、或、异或和非操作

位图的缺点:
只支持连续自然数值范围,非连续自然数值范围时需要额外信息来协助使用。

上一篇下一篇

猜你喜欢

热点阅读