Interpretation of bitmap type of Redis

Table of contents

basic introduction

basic command 

Setbit 

Getbit 

BITCOUNT 

Application Scenario

Statistics of active users of the day

user sign in

bitmap - Redis bloom filter (to deal with cache penetration problem)


basic introduction

Redis's bitmap (bitmap) is an array composed of multiple binary bits. There are only two states, 0 and 1. Each binary bit in the array has a corresponding offset (starting from 0), through these Offsets can operate on one or more bits specified in the bitmap.

Bitmap can be imagined as an array in units of bits . Each unit of the array can only store 0 and 1. The subscript of the array is called offset in Bitmap, and the default value of bitmap is 0.

 The bottom layer of BitMap actually uses strings for storage. Since the maximum length of strings in Redis is 512 MB, the offset value of BitMap is also limited. The maximum value is: 8 * 1024 * 1024 * 512 = 2^32 (4G).

The basic principle of BitMap is to use a bit to mark the Value corresponding to an element, and the Key is the element. Since one bit is used to store one data, the space can be greatly saved.

The smallest unit allocated to memory by the computer is bit, 1Byte=8bit, and 1 integer type is 4Byte=32bit.

basic command 

Common commands effect
1、getbit key offset It is used to obtain the value corresponding to the specified key in Redis, and the bit corresponding to the offset in Redis
2、setbit key key offset value It is used to modify the value corresponding to the specified key, and the bit corresponding to offset in
3、 bitcount key [start end] The number of bits used to count the string is set to 1
4、bitop and/or/xor/not destkey key [key …] Used to perform logical AND/logical OR/logic XOR/logic NOT for multiple keys

Setbit 

The Redis Setbit command is used to set or clear the bit (bit) at the specified offset for the string value stored in the key.

redis> SETBIT bit 10086 1
(integer) 0
 
redis> GETBIT bit 10086
(integer) 1
 
redis> GETBIT bit 100   # bit 默认被初始化为 0
(integer) 0

Return value: The bit where the specified offset was originally stored.

Getbit 

The Redis Getbit command is used to obtain the bit (bit) at the specified offset for the string value stored in the key.

# 对不存在的 key 或者不存在的 offset 进行 GETBIT, 返回 0
 
redis> EXISTS bit
(integer) 0
 
redis> GETBIT bit 10086
(integer) 0
 
 
# 对已存在的 offset 进行 GETBIT
 
redis> SETBIT bit 10086 1
(integer) 0
 
redis> GETBIT bit

Return value: A string value specifying the bit at the offset. Returns 0 when offset OFFSET is greater than the length of the string value, or key does not exist.

BITCOUNT 

Count the number of 1s in the specified bit range. The syntax format is as follows:

BITCOUNT key [start end]

127.0.0.1:6379> BITCOUNT user:1

(integer) 8

Only specific bytes can be counted by specifying the start and end parameters . The start and end parameters are similar to the parameters of the GETRANGE command, both of which can use negative numbers, such as -1 means the first last bit, and -2 means the second last bit.

Application Scenario

Statistics of active users of the day

Create a bitmap key for daily active statistics. When the user is active, set the corresponding bit to 1 according to the offset of the user id

user sign in

Each user creates a bitmap key, based on a certain day, and the number of days after that is the offset from this day. If the user clicks on the sign-in, set the corresponding offset to 1.

bitmap - Redis bloom filter (to deal with cache penetration problem)

Example: For example, when crawling the product information of an e-commerce website, the crawler server first passes through the cache. If the cache cannot be found, it then goes to the database to obtain the information, because the crawler is very efficient, and the SKU may not exist or has been downloaded. If it is framed, it will cause cache penetration, and a large number of requests will be sent to the database, causing the server to be affected.

At this point, you can add a Bloom filter before the cache layer. The Bloom filter is regarded as a bitmap, and the sku is used as the offset value. If the product actually exists, the bit value is set to 1. First initialize the product data, and when there is a request, use getbit to determine whether the sku is valid. This protects the storage layer by denying access if the Bloom filter believes that the item does not exist.

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/132424914