当前位置:网站首页>Bitmap of redis data structure

Bitmap of redis data structure

2022-06-24 14:41:00 Sword Saint without trace

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 29 God , Click to see the event details

background

Project development process , We often use boolean Type to store data . For example, record the daily check-in of users , Check in yes 1, Otherwise 0, If we need to count the number of sign ins in a year , If the String To store , Every user needs to record 365 Time , When the number of users is very large , The storage space required is huge . To solve this problem ,Redis Bitmap data structure is provided to solve this problem .

brief introduction

bitmap Bitmap for short , Is an array of multiple binary bits , Each binary bit in the array has its corresponding offset , These offsets can be used to manipulate one or more binary bits specified in the bitmap .

data structure

 picture .png

explain : A bitmap can also be viewed as an array of bytes , The index sequence number represents the corresponding value ,bitmap The default value is zero 0

Basic commands

 picture .png

setbit Set the value of the binary bit

Basic grammar

setbit key offset value

Be careful : Set up bit When the value of , among offset The value of cannot be negative , Otherwise it will be reported ERR bit offset is not integer or out of range error .

example

Place bitmap mbit Set to :10010100

  # Set the first position to 1
  setbit mbit 0 1 
  # Set the fourth position to 1
  setbit mbit 3 1 
  # Set the sixth position to 1
  setbit mbit 5 1 

The changing process of bitmap

 picture .png

Bitmap extension

When the user executes setbit On command , If the bitmap does not exist , Or the current size of the bitmap cannot meet the setting operation that the user wants to perform , that Redis The set bitmap will be extended , So that the bitmap can meet the user's setting request .

For example, the user executes the following command :

setbit mbit 10 1

Redis The bitmap created is not just 11 Binary bits , Instead, there are two bytes in total 16 Binary bits , As shown in the figure below .

 picture .png

explain : We can also see from the picture , Except that the offset is 10 Outside the binary bit of , All other bits that are not set are initialized to 0.

getbit Get binary value

Basic grammar

getbit key offset

Example

 picture .png

bitcount Count the number of binary bits set

Basic grammar

bitcount key start end

Example

 picture .png

bitop For one or more strings that hold binary bits key Perform bit operation

Basic grammar

bitop and key1 key2

explain : bitop Command support and、or、not、xor These four operations

  • and: And operators (&) Two at the same time 1, The result is 1, Otherwise 0
  • or: Or operations (|) One for 1, Its value is 1
  • not: Take the opposite (0110 0001 NOT: 1001 1110)
  • xor: Exclusive or operation , Values differ by 1, Otherwise 0

Example

 picture .png

bitpos Used to find the first... In the specified range 0 or 1 ( Bytes are units )

Basic grammar

bitpos key start end

Example

 picture .png

Application scenarios

  • 1. User sign in times

1 Representatives sign in ,0 The representative didn't sign in , In this way, you can easily count the activity of users . Compared to using strings directly , Each record in the bitmap occupies only one bit position , This greatly reduces the memory space utilization .

2. Count login times

summary

This article explains the basic data structure and operation commands of bitmap , Bitmap is suitable for some specific scenes , We need to gather the actual business scenarios , Choosing the right data structure storage can greatly reduce Redis Of memory space .

原网站

版权声明
本文为[Sword Saint without trace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241413527209.html